Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739

Similar presentations


Presentation on theme: "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739"— Presentation transcript:

1 CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739 alphonce@buffalo.edu

2 Announcements Exam 3 on Wednesday 11/10 –covers material from last exam up to and including Friday 11/05 Exam review in lecture on Monday 11/08 –study over the weekend, and bring questions to class on Monday Sample questions on web-site over the weekend

3 Agenda Collections: quick review Inheritance – our last relationship! –we will only introduce inheritance today –we’ll continue after the exam

4 Collections: concepts interface: java.util.Collection –E is the type of element contained in the collection – replace by an actual type concrete implementations (examples): –java.util.HashSet at most one of each internal order irrelevant to client –java.util.ArrayList multiples permitted maintains order; client can determine order

5 Collections: usage - 1 To declare a variable of type HashSet of String: HashSet names; To create a HashSet of String object, and assign its reference to the variable declared above: names = new HashSet ();

6 Collections: usage - 2 To add a String to the HashSet: names.add(“Fred”); To remove a String from the HashSet: names.remove(“Fred”);

7 Collections: usage - 3 for-each loop – process each element of a collection of elements of type E: for (E item : collection) { // do something with item } Example: to print out each String in names: for (String name : names ) { System.out.println(name); } Declared as: HashSet Declared as: HashSet

8 Example from Monday

9 What’s wrong with this code? package noninheritance; public class Cat implements Noisy { private String _myName; public Cat(String n) { _myName = n; } public String getName() { return _myName; } @Override public String sound() { return "meow"; } package noninheritance; public class Dog implements Noisy { private String _myName; public Dog(String n) { _myName = n; } public String getName() { return _myName; } @Override public String sound() { return ”ruff"; }

10 Code duplication a “code smell” package noninheritance; public class Cat implements Noisy { private String _myName; public Cat(String n) { _myName = n; } public String getName() { return _myName; } @Override public String sound() { return "meow"; } package noninheritance; public class Dog implements Noisy { private String _myName; public Dog(String n) { _myName = n; } public String getName() { return _myName; } @Override public String sound() { return ”ruff"; }

11 Number one in the stink parade is duplicated code. If you see the same code structure in more than one place, you can be sure that your program will be better if you find a way to unify them. Refactoring: Improving the Design of Existing Code, Martin Fowler, page 76

12 Inheritance Inheritance is the last of the relationships we will study this semester. It is a relationship between: –two classes –two interfaces Inheritance is (syntactically) simple Inheritance is (conceptually) messy

13 original design

14 class to class inheritance I

15 In code: public abstract class Noisy {…} public class Cat extends Noisy {…}

16 Abstract class A class which mixes method specifications (abstract methods) with fully defined methods (concrete methods) is abstract. An interface contains only abstract methods (they are labelled ‘abstract’ implicitly).

17 Abstract class ‘abstract’ keyword in class header cannot be instantiated

18 Inheritance (“extends”) Source class: –subclass –child class –derived class Target class: –superclass –parent class –base class

19 Implications of “extends” Same type implications as for interfaces: –instance of subclass belongs to subclass type and superclass type inheritance: non-private members of superclass can be accessed via subclass object. –e.g. it’s as if methods of superclass were defined in subclass

20 [A] common duplication problem is when you have the same expression in two sibling subclasses. You can eliminate this duplication by using Extract Method (110) in both classes then Pull Up Method (322). Refactoring: Improving the Design of Existing Code, Martin Fowler, page 76

21 class to class inheritance IIa

22 Code duplication a “code smell” package noninheritance; public class Cat implements Noisy { private String _myName; public Cat(String n) { _myName = n; } public String getName() { return _myName; } @Override public String sound() { return "meow"; } package noninheritance; public class Dog implements Noisy { private String _myName; public Dog(String n) { _myName = n; } public String getName() { return _myName; } @Override public String sound() { return ”ruff"; }

23 Refactored code (-: a breath of fresh air :-) package inheritance; public class Cat extends Noisy { public Cat(String n) { super(n); } @Override public String sound() { return "meow"; } package inheritance; public class Cat extends Noisy { public Dog(String n) { super(n); } @Override public String sound() { return ”ruff"; } package inheritance; public abstract class Noisy { private String _myName; public Noisy(String name) { _myName = name; } public abstract String sound(); public String getName() { return _myName; }


Download ppt "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739"

Similar presentations


Ads by Google