scjp party

14
SCJP Party Chapter 2: Object Orientation

Upload: genna

Post on 13-Jan-2016

45 views

Category:

Documents


1 download

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

Page 1: SCJP Party

SCJP PartyChapter 2: Object Orientation

Page 2: SCJP Party

Objectives

• Inheritance

• Overload and Override

• Constructors

• Statics

Page 3: SCJP Party

Inheritance

• Java supports only single inheritance!

Page 4: SCJP Party

Inheritance

Page 5: SCJP Party

Inheritances

Page 6: SCJP Party

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

Page 7: SCJP Party

Overridden - Example

Page 8: SCJP Party

Overridden - Example

Page 9: SCJP Party

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.

Page 10: SCJP Party

Differences

Page 11: SCJP Party

• Examples on Eclipse

Page 12: SCJP Party

• 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

Page 13: SCJP Party

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.

Page 14: SCJP Party

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