scjp party

Post on 13-Jan-2016

45 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

SCJP PartyChapter 2: Object Orientation

Objectives

• Inheritance

• Overload and Override

• Constructors

• Statics

Inheritance

• Java supports only single inheritance!

Inheritance

Inheritances

Overridden Methods

• 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 - Example

Overridden - Example

Overloaded Methods

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

Differences

• Examples on Eclipse

• 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

Constructors

• 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.• Interfaces do not have constructors. Interfaces are not part of

an object's inheritance tree.

Static

• 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

top related