110 likes | 225 Views
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. Exam 4. Monday (12/6) Exam 4 review Wednesday (12/8) Exam 4 Friday (12/10) Final exam review. This week. Primitives (floating point numbers)
E N D
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
Exam 4 • Monday (12/6) Exam 4 review • Wednesday (12/8) Exam 4 • Friday (12/10) Final exam review
This week • Primitives (floating point numbers) • Inheritance (tie up loose ends) • Lab discussion • maybe on Friday, if time allows
Agenda • Inheritance details • object construction • super • constructor chaining
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.
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 extended 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. • Call to superclass constructor is ALWAYS the first statement. If not, compiler complains.
public class A { private int _x; public A() { _x = 5; } public A(intx) { _x = x; } } public class B extends A { private String _n; public B() { super(3); _n = “Yes!”; } } public class C extends A { private double _n; private String _x; public C() { super(); _x= “Oh?”; _n = 3.14; } }