110 likes | 268 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. Announcements. Register for CSE116 if you haven’t already. Agenda. Today: Inheritance overriding of methods (full, partial, none) Primitives
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
Announcements • Register for CSE116 if you haven’t already.
Agenda • Today: • Inheritance • overriding of methods (full, partial, none) • Primitives • integral, floating point, boolean • representations • wrapper classes • Coming up: • More control structures
Last time • Different kinds of extension in Java • class to class • Object is root of Java’s singly-rooted class hierarchy • single inheritance vs. multiple inheritance • multiple implementations can conflict • interface to interface • multiple inheritance OK here • multiple specifications don’t conflict • Implementation of interfaces • a class can implement an arbitrary number of interfaces • multiple specifications don’t conflict
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.
A few loose ends… • The next several slides ties up a few loose ends.
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.