90 likes | 213 Views
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. inheritance.
E N D
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
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())
total 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{ @Override 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 { @Override 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.
constructor and method overloading • A class can define multiple methods with the same name, as long as they differ in their parameter lists. • A class can therefore define multiple constructors (which all MUST share their name), as long as they differ in their parameter lists. • Providing multiple method definitions with the same name is called overloading: the name is overloaded with multiple definitions. Selection of correct definition is based on the argument list in a given method call.
overloading vs. overriding • overloading: • same name is used for many different method definitions • parameter lists of methods must all be different • all definitions co-exist • overriding: • same name is used for a subclass method also defined in a superclass • parameter list must be exactly the same in subclass definition as in superclass definition • subclass definition supplants superclass definition
constructor overriding • Constructors can (and should generally) be overridden when extending classes other than Object. • A superclass constructor is always called, implicitly if not explicitly. Implicit call is to super(). • Use super with argument list to explicitly call one of the superclass’ other constructors. (Cf. use of this to call another constructor in same class.) • Call to superclass constructor is ALWAYS the first statement. If not, compiler complains.