1 / 18

CSE115: Introduction to Computer Science I

CSE115: Introduction to Computer Science I. Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu. Agenda. Inheritance – our last relationship! Primitives & control structures. Example. What’s wrong with this code?. package noninheritance ; public class Cat implements Noisy {

ovidio
Download Presentation

CSE115: Introduction to Computer Science I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu

  2. Agenda • Inheritance – our last relationship! • Primitives & control structures

  3. Example

  4. What’s wrong with this code? package noninheritance; public class Cat implements Noisy { private String _myName; public Cat(Stringn) { _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(Stringn) { _myName = n; } public String getName() { return _myName; } @Override public String sound() { return ”ruff"; } }

  5. Code duplicationa “code smell” package noninheritance; public class Cat implements Noisy { private String _myName; public Cat(Stringn) { _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(Stringn) { _myName = n; } public String getName() { return _myName; } @Override public String sound() { return ”ruff"; } }

  6. 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

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

  8. original design

  9. class to class inheritance I

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

  11. 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).

  12. Abstract class • ‘abstract’ keyword in class header • cannot be instantiated

  13. Inheritance(“extends”) • Source class: • subclass • child class • derived class • Target class: • superclass • parent class • base class

  14. 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

  15. [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

  16. class to class inheritance IIa

  17. Code duplicationa “code smell” package noninheritance; public class Cat implements Noisy { private String _myName; public Cat(Stringn) { _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(Stringn) { _myName = n; } public String getName() { return _myName; } @Override public String sound() { return ”ruff"; } }

  18. Refactored code(-: a breath of fresh air :-) package inheritance; public class Cat extends Noisy { public Cat(Stringn) { super(n); } @Override public String sound() { return "meow"; } } package inheritance; public class Dog extends Noisy { public Dog(Stringn) { 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; } }

More Related