Inheritance in JAVA

 

Inheritance


• Methods allows a software developer to reuse a sequence of

statements

• Inheritance allows a software developer to reuse classes by

deriving a new class from an existing one

• The existing class is called the parent class, or superclass, or

base class

• The derived class is called the child class or subclass.

• As the name implies, the child inherits characteristics of the

parent

• That is, the child class inherits the methods and data defined

for the parent class Inheritance relationships are often shown

graphically in a class diagram, with the arrow pointing to the parent class.



Deriving Subclasses


In Java, we use the reserved word extends to establish an

inheritance relationship

class Animal

{

// class contents

int weight;


public void int getWeight() {...}


}


class Bird extends Animal

{

// class contents


public void fly() {...};


}


Forms of Inheritance


Inheritance is used in a variety of way and for a variety of different

purposes .

• Inheritance for Specialization

• Inheritance for Specification

• Inheritance for Construction

• Inheritance for Extension

• Inheritance for Limitation

• Inheritance for Combination

One or many of these forms may occur in a single case.


Summary of Forms of Inheritance


Specialization. The child class is a special case of the parent class; in other words, the child class is a subtype of the parent class.

Specification. The parent class defines behavior that is implemented in the child class but not in the parent class.

Construction. The child class makes use of the behavior provided by the parent class, but is not a subtype of the parent class.

Generalization. The child class modifies or overrides some of the methods of the parent class.

Extension. The child class adds new functionality to the parent class, but does not change any inherited behavior.

Limitation. The child class restricts the use of some of the behavior inherited from the parent class.


Variance. The child class and parent class are variants of each other, and the class- subclass relationship is arbitrary.


Combination. The child class inherits features from more than one parent class. This is multiple inheritance and will be the subject of a later chapter.



The Benefits of Inheritance


• Software Reusability (among projects)

• Increased Reliability (resulting from reuse and sharing of

well-tested code)

• Code Sharing (within a project)

• Consistency of Interface (among related objects)

• Software Components

• Rapid Prototyping (quickly assemble from pre-existing

components)

• Polymorphism and Frameworks (high-level reusable

components)

• Information Hiding


The Costs of Inheritance


• Execution Speed


• Program Size


• Message-Passing Overhead


• Program Complexity (in overuse of inheritance)


Using final with inheritance


• final keyword is used declare constants which can not change its value of definition.


• final Variables can not change its value.


• final Methods can not be Overridden or Over Loaded


• final Classes can not be extended or inherited


Preventing Overriding with final


• A method declared final cannot be overridden in any

sub-class:

class A {

final void meth() {

System.out.println("This is a final method.");

}

}

This class declaration is illegal:

class B extends A {

void meth() {

System.out.println("Illegal!");

}

}



Preventing Inheritance with final


• A class declared final cannot be inherited – has no sub-

classes.


final class A { ... }


• This class declaration is considered illegal:


class B extends A { ... }


• Declaring a class final implicitly declares all its methods

final.


• It is illegal to declare a class as both abstract and final.


Post a Comment (0)
Previous Post Next Post