Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739

Similar presentations


Presentation on theme: "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739"— 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 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())

4 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); }

5 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); }

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

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

8 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

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


Download ppt "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739"

Similar presentations


Ads by Google