290 likes | 394 Views
CSE115: Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis 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 {
E N D
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu
Agenda • Inheritance – our last relationship! • Primitives & control structures
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"; } }
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"; } }
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
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
In code: public abstract class Noisy {…} public class Cat extends Noisy {…} public class Dog extends Noisy {…}
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).
Abstract class • ‘abstract’ keyword in class header • cannot be instantiated
Inheritance(“extends”) • Source class: • subclass • child class • derived class • Target class: • superclass • parent class • base class
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
[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
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"; } }
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; } }
interface inheritance • In Java, an interface can extend zero or more interfaces: • extending zero interfaces public interface A { public void foo(); } • extending one interface public interface B extends A { public void bar(A a); } • extending many interfaces public interface C extends B, ActionListener { public ActionEventgetEvent(); }
implements and extends clauses • a class can implement an arbitrary number of interfaces • a (user-defined) class extends exactly one class • Object by default
class (implementation) inheritance • A (user-defined) class always extends exactly one (other) class: public class Circle extends Shape {…} • If class header has no explicit extends clause, parent class is implicitly Object: public class Shape {…} public class Shape extends Object {…} • Object is the root of Java’s class hierarchy.
implements clause • a class can implement an arbitrary number of interfaces • type hierarchy vs. class hierarchy • class: single root – Object • type: many roots • all instantiable types fall under Object all objects are of type Object
interface inheritance,cont’d • When an interface A extends another interface B, A inherits the methods specified by B. • This means that a class which implements A must define all the methods specified in both A and B. • If an interface can have at most one specification for any given method: even if an interface inherits the very same method specification (same name, same parameter list) from two or more parent interfaces, the interface has the method specified just once.
What is inherited? • Given what we know, a correct answer is that anything that is not private is inherited. • All our properties (instance variables) are private, so they are not inherited. • All our methods are public (not private), so they are inherited.
What is effectof inheritance? • A method inherited from a superclass to a subclass can be invoked on a subclass instance, even though not defined there: public class Foo { private Bar _bar; public void setBar(Bar b) { _bar = b; } } public class FooSub extends Foo { … } • This is legal: new FooSub().setBar(new Bar())
multiple inheritance • Some languages allow multiple (implementation) inheritance (e.g. C++) • Java does not (but Java has interfaces) • Issue: • if the same method, defined in different ways, is inherited from different ancestors, which implementation has priority?
Method overriding • A subclass can override a definition inherited from superclass • How: by providing an alternate definition. public class Foo { private Bar _bar; public void setBar(Bar b) { _bar = b; } } public class FooSub extends Foo { public void setBar(Bar b) { b.setColor(java.awt.Color.CYAN); } }
partial overriding • A subclass can add something to a definition inherited from superclass simply first invoking the superclass’ definition, then adding extra code in an augmenting definition of the method in the subclass’ definition: public class Foo { private Bar _bar; public void setBar(Bar b) { _bar = b; } } public class FooSub extends Foo { public void setBar(Bar b) { super.setBar(b); // let superclass method do: _bar = b; b.setColor(java.awt.Color.CYAN); } }
overriding summary • total (complete) overriding • a subclass provides an entirely new definition for a method which would otherwise have been inherited from the superclass • partial overriding • a subclass provides a definition for a method which would otherwise have been inherited from the superclass, but calls the superclass version via super. • inheritance • a subclass does not provide an alternate defintion for a method defined in the superclass, which is inherited.