1 / 11

CSE115: Introduction to Computer Science I

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

ayita
Download Presentation

CSE115: Introduction to Computer Science I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Announcements • Register for CSE116 if you haven’t already.

  3. Agenda • Today: • Inheritance • overriding of methods (full, partial, none) • Primitives • integral, floating point, boolean • representations • wrapper classes • Coming up: • More control structures

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

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

  6. 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); } }

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

  8. A few loose ends… • The next several slides ties up a few loose ends.

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

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

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

More Related