1 chapter 6 inheritance, interfaces, and abstract classes

Post on 20-Dec-2015

218 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Chapter 6

Inheritance, Interfaces, and Abstract Classes

2

Inheritance basics

• Every class except Object extends (that is, is a subclass of) exactly one other class.

• Java supports only single inheritance for classes; that is, a class can have only one superclass.

• Java supports multiple inheritance for interfaces; that is, an interface can have more than one superinterface.

3

Inheritance basics

• If a class is declared without an explicit superclass, Object is the superclass.

• The code segment class C {/*...*/}

is thus shorthand for

class C extends Object {/*...*/}

4

Inheritance basics

• The general syntax for inheritance is

class Mang extends Emp {/*...*/} interface I3 extends I1, I2 {/*...*/}

• A subclass inherits all superclass members except for constructors.

• A subinterface inherits all superinterface members.

5

Inheritance basics

• Because Object is at the root of the class inheritance hierarchy in Java, Object has no superclass.

• Every other class, standard or programmer defined, has Object as an ancestor.

• Object members such as the toString() method are inherited by all Object descendants.

6

Inheritance and scope

• Scope rules apply in inheritance. For instance, given class Child extends Parent {/*...*/}

Parent must have either public scope or be in the same package as Child.

7

Inheritance and scope

• Inherited members normally retain their scope. For instance, a superclass member with private scope is inherited with private scope by the subclass.

• An inherited member’s scope can be increased but not decreased. For example, an inherited protected member can be redeclared as public but not vice-versa.

8

Inheritance and scope

• The clone method in Object has protected scope. Object subclasses are allowed to redeclare clone as public in accordance with the rule that an inherited member’s scope can be increased but not decreased.

9

Constructors under inheritance

• Although constructors are not inherited, they play a key role in inheritance.

• Every subclass constructor must invoke a superclass constructor, either implicitly or explicitly.– The invocation must occur as the first statement

in the subclass constructor.

10

Constructors under inheritance

• class C extends Object { C(){ n = -999; }

int n;

}

is shorthand for class C extends Object {

C(){ super(); n = -999;}

int n;

}

11

Constructors under inheritance

• If the programmer does not explicitly invoke a superclass constructor in a subclass constructor, the compiler in effect inserts super() as the first statement.

• super()invokes the superclass no-argument constructor. super(-1)invokes a superclass parameterized constructor.

12

Constructors under inheritance

• Because a subclass derives from a superclass, it is appropriate that every subclass constructor invoke a superclass constructor to handle construction of the inherited or “superclass part” of a subclass object.

13

Name hiding

• Within a class, every field must have a distinct name.

• In an inheritance hierarchy, a subclass field may have the same name (e.g., num) as a superclass field. In this case, the subclass field hides the superclass field of the same name. The term super can be used to access the superclass field: super.num.

14

Name hiding

• A subclass method may have the same signature as a superclass method. In this case, the subclass method overrides the superclass method of the same name.– It is common for Object descendants to

override the toString() method inherited ultimately from Object.

• Overriding is deliberate name hiding.

15

Disabling inheritance

• A class can be declared as final prevent subclassing.

• The standard String class is declared as final; hence, no standard or programmer-defined class can extend String.

16

Inheritance and casts

• A reference of a superclass type can refer to an object of a subclass type:

Object obj = new Date();

• A reference of a subclass type can refer with casting to an object of a superclass type, but this is very dangerous: Date d = (Date) new Object();

17

Inheritance and casts

• A reference of one peer type cannot refer, even with explicit casting, to an object of another type peer. Peer types are classes between which there is no ancestor/descendant relationship.

Date d = (Date) new String(); // error

18

Inheritance and casts

• Casting a reference to subclass type (e.g., Date) so that the reference can refer to an object of a superclass type (e.g., Object) is known as a down cast.

• Down casts are not type safe; that is, such casts can lead to subtle run-time errors.

19

Polymorphism

• Polymorphism has two requirements:– A subclass method must override a superclass

method. Only superclass methods with public and protected scope are eligible for overriding.

– A superclass reference, which may refer to either a superclass or a subclass object, is used to invoke the method.

20

Polymorphism

• The code segment Object obj;

if ( new Random().nextInt() > 0 )

obj = new Date();

else

obj = new Vector();

System.out.println( obj.toString() );

illustrates a polymorphic invocation.

21

Polymorphism

• All Components such as windows, buttons, and labels inherit a setVisible method that can be overridden to display the component in a class-appropriate fashion. If com refers to any Component, then comp.setVisible( true ) polymorphically invokes the setVisible method.

22

Review of overriding

• Only protected and public methods can be overriden.

• Only nonstatic methods can be overridden.

• Methods declared as final cannot be overriden.

23

Interface basics

• An interface is defined with the keyword interface:

interface MyInterface { public void m1();

public String m2();

}

• An interface cannot be instantiated as an object.

24

Interface basics

• An interface can contain– method declarations rather than definitions– static and final fields, which represent constants

•public static final double pi = 3.1415;

• All interface members must be declared public.

• Empty or marker interfaces are allowed.

25

Interface basics

• A class implements an interface:

class C implements Runnable {

• A class can implement multiple interfaces. The interface identifiers are separated by commas.

• One interface can extend (subclass) one or more interfaces.

26

Interfaces and concrete classes

• If a class implements an interface, the class must define all of the methods declared in the interface in order to be concrete.– A concrete class is a class that can be

instantiated as an object.– An abstract class is a class that cannot be

instantiated as an object.

27

Interface basics

• An interface, like a class, has either package or public scope. Package scope is the default.

• An interface may be declared abstract abstract interface MyIface {/*...*}

to emphasize that the interface cannot be instantiated as an object.

28

Interface basics

• All interfaces are abstract even if not declared so. The term abstract signals that an interface cannot be instantiated as an object.

• Interface methods may be declared abstract to emphasize that interface methods are only declared rather than defined.

29

Interfaces and concrete classes

• The Runnable interface declares one method

public void run();

A class that implements Runnable must define run in order to be concrete.

30

Interface types and references

• Object references can be of interface types. The code segment

Set s1 = new HashSet();

illustrates. Set is an interface rather than a class.

• Because s1 is an interface type, it can be used to invoke only methods declared in the Set interface.

31

Interface types and references

• “Programming to the interface” is a key principle in object-oriented programming. Wherever possible, object references should be of interface types.

• In JDK documentation, interfaces are listed before classes. This underscores that classes should be viewed, wherever possible, as interface implementations.

32

Nested interfaces

• An interface can be nested inside a class. In this case, the interface is static regardless of whether it is declared so.

33

Abstract classes

• An abstract class cannot be instantiated as an object:

abstract class ABS {/*...*/} ABS a1 = new ABS(); //*** ERROR

• Abstract classes can contain an arbitrary mix of method declarations and method definitions.

34

Abstract classes

• There are three ways for a class to be abstract:– The class is explicitly declared abstract.– An encapsulated method is explicitly declared

abstract.– The class fails to define all of the methods in

the interfaces that the class implements.

35

Abstract classes

• Abstract classes are bona fide classes. For instance, such classes descend from Object, have exactly one superclass, and so on.

• Abstract classes tend to have highly specialized uses. Beginning programmers typically use either concrete classes or interfaces.

top related