1 / 14

SCJP Party

SCJP Party. Chapter 2: Object Orientation. Inheritance Overload and Override Constructors Statics. Objectives. Java supports only single inheritance!. Inheritance. Inheritance. Inheritances. The argument list must exactly match that of the overridden method

genna
Download Presentation

SCJP Party

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. SCJP Party Chapter 2: Object Orientation

  2. Inheritance • Overload and Override • Constructors • Statics Objectives

  3. Java supports only single inheritance! Inheritance

  4. Inheritance

  5. Inheritances

  6. The argument list must exactly match that of the overridden method • The return type must be the same as, or a subtype of, the return type declared in the original overridden method in the superclass. • The access level can't be more restrictive than the overridden method's. • The overriding method CAN throw any unchecked (runtime) exception • The overriding method must NOT throw checked exceptions that are new • or broader than those declared by the overridden method Overridden Methods

  7. Overridden - Example

  8. Overridden - Example

  9. Overloaded methods MUST change the argument list. • Overloaded methods CAN change the return type. • Overloaded methods CAN change the access modifier. • Overloaded methods CAN declare new or broader checked exceptions. Overloaded Methods

  10. Differences

  11. Examples on Eclipse

  12. Just remember, the reference type (not the object type) determines which overloaded method is invoked! • To summarize, which overridden version of the method to call (in other words, from which class in the inheritance tree) is decided at runtime based on object type, but which overloaded version of the method to call is based on the reference type of the argument passed at compile time

  13. Constructors can use any access modifier, including private. • The constructor name must match the name of the class. • Constructors must not have a return type • The default constructor is ALWAYS a no-arg constructor. • The first line in a constructor must be a call to super() or a call to this() • You cannot make a call to an instance method, or access an instance variable, until after the super constructor runs. • Abstract classes have constructors, and those constructors are always called when a concrete subclass is instantiated. • Interfacesdo not have constructors. Interfaces are not part of an object's inheritance tree. Constructors

  14. Static methods can't be overridden • Static method cannot access an instance (non-static) variable • Static method cannot access a non-static method • Static method can access a static method or variable Static

More Related