Unit 4-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Inheritance

By. Dr. Kiran K. Tangod


Prof. & Head
Dept of ISE, GIT, Belagavi
Inheritance
• Inheritance is one of the cornerstones of object-oriented programming
• It allows the creation of hierarchical classifications.
• Using inheritance, general class can be created that defines traits common
to a set of related items. This class can then be inherited by other, more
specific classes, each adding those things that are unique to it.
• In the terminology of Java, a class that is inherited is called a superclass.
The class that does the inheriting is called a subclass.
• A subclass is a specialized version of a superclass. It inherits all of the
instance variables and methods defined by the superclass and adds its own,
unique elements.
Extends keyword // A simple example of inheritance.
// Create a superclass.
class A {
• To inherit a class, you int i, j;
void showij() {
simply incorporate the System.out.println("i and j: " + i + " " + j);
definition of one class }
}
into another by using // Create a subclass by extending class A.
the extends keyword class B extends A {
int k;
The general form of a class declaration that void showk() {
inherits a superclass is shown here: System.out.println("k: " + k);
class subclass-name extends superclass- }
name void sum() {
{ System.out.println("i+j+k: " + (i+j+k));
}
// body of class
}
}
Using super
• super Keyword is used in a subclass to refer to its immediate
superclass.
• super has two general forms.
1. The first calls the superclass’ constructor.
2. The second is used to access a member of the superclass that has been
hidden by a member of a subclass.
Using super to Call Superclass Constructors
• A subclass can call a constructor defined by its superclass by use of
the following form of super
super(arg-list);
• arg-list specifies any arguments needed by the constructor in the
superclass.

• super( ) must always be the first statement executed inside a


subclass’ constructor.
Example: super () used for calling base class
constructor

class BoxWeight extends Box {


double weight; // weight of box
// initialize width, height, and depth using super()
BoxWeight(double w, double h, double d, double m) {
super(w, h, d); // call superclass constructor
weight = m; //initialize only the new instance variable in subclass
}
}
A Second Use for super

• The second form of super acts somewhat like this, except that it
always refers to the superclass of the subclass in which it is used.
• This usage has the following general form:
super.member
• member can be either a method or an instance variable.
• This second form of super is most applicable to situations in which
member names of a subclass hide members by the same name in the
superclass.
Example: accessing superclass members using super
keyword

void show() {
class A {
System.out.println("i in superclass: " + sup
int i;
i);
}
System.out.println("i in subclass: " + i);
// B issubclass by extending class A. }
class B extends A { }//endof class B
int i; // this i hides the i in A class UseSuper {
B(int a, int b) { public static void main(String args[]) {
super.i = a; // i in A B subOb = new B(1, 2);
i = b; // i in B subOb.show(); Output:
} } i in superclass: 1
i in subclass: 2
}
Creating a Multilevel Hierarchy
BoxX

BoxWeight

Shipment

• In this class hierarchy subclass BoxWeight is used as a superclass to


create the subclass called Shipment. Shipment inherits all of the
traits of BoxWeight and Box, and adds a field called cost, which holds
the cost of shipping such a parcel.
• Implementation
When Constructors Are Called
• In a class hierarchy, constructors are called in order of derivation,
from superclass to subclass.
• Further, since super( ) must be the first statement executed in a
subclass’ constructor, this order is the same whether or not super( )is
used.
• If super( ) is not used, then the default or parameter less constructor
of each superclass will be executed.
• Implementation
Method Overriding
• In a class hierarchy, when a method in a subclass has the same name
and type signature as a method in its superclass, then the method in
the subclass is said to override the method in the superclass.
• When an overridden method is called from within a subclass, it will
always refer to the version of that method defined by the subclass.
• The version of the method defined by the superclass will be hidden.

• Example
Dynamic Method Dispatch
• Dynamic method dispatch is the mechanism by which a call to an
overridden method is resolved at run time, rather than compile time.
• Dynamic method dispatch implements run-time polymorphism
• When different types of objects are referred to, different versions of
an overridden method will be called.
• The type of the object being referred to (not the type of the
reference variable) that determines which version of an overridden
method will be executed.

• Example
Using Abstract Classes
• Abstract class is a superclass that declares the structure of a given
abstraction without providing a complete implementation of every
method.
• It defines a generalized form that will be shared by all of its
subclasses, leaving it to each subclass to fill in the details.
• This is useful when a superclass is unable to create a meaningful
implementation for a method.
Figure
General form of abstract method declaration: (Abstract class)
abstract type name(parameter-list);
Triangle Rectangle
Using Abstract Classes
• Any class that contains one or more abstract methods must also be
declared abstract.
• To declare a class abstract, abstract keyword is uaed in front of the
class keyword at the beginning of the class declaration.
• There can be no objects of an abstract class.
• That is, an abstract class cannot be directly instantiated with the new
operator.
• Such objects would be useless, because an abstract class is not fully
defined.
• Example
Using final with Inheritance
• The keyword final has three uses.
• First, it can be used to create the equivalent of a named constant.
This use was described in the preceding chapter.
• The other two uses of final apply to inheritance.
• Using final to Prevent Overriding (method level)
• Using final to Prevent Inheritance (class level)
Using final to Prevent Overriding
• final can be used to disallow a method from being overridden
• If final is specified as a modifier at the start of its declaration, such
methods cannot be overridden.
Example:
class A {
final void meth() {
System.out.println("This is a final method.");
}
} class B extends A {
void meth() { // ERROR! Can't override.
System.out.println("Illegal!");
}
}
Special features of final methods (inline)
• Methods declared as final can sometimes provide a performance
enhancement: The compiler is free to inline calls to them because it
“knows” they will not be overridden by a subclass.
• When a small final method is called, often the Java compiler can copy
the bytecode for the subroutine directly inline with the compiled
code of the calling method, thus eliminating the costly overhead
associated with a method call.
• Inlining is only an option with final methods. Normally, Java resolves
calls to methods dynamically, at run time. This is called late binding.
However, since final methods cannot be overridden, a call to one can
be resolved at compile time. This is called early binding.
Using final to Prevent Inheritance
• To prevent a class from being inherited final can be used.
• Declaring a class as final implicitly declares all of its methods as final,
too.
• It is illegal to declare a class as both abstract and final since an
abstract class is incomplete by itself and relies upon its subclasses to
provide complete implementations.
Example : final class

final class A {
// ...
}
// The following class is illegal.
class B extends A { // ERROR! Can't subclass A
// ...
}

As the comments imply, it is illegal for B to inherit A since A is declared as final.


The Object Class
• There is one special class, Object, defined by Java. All other classes
are subclasses of Object.
• Object is a superclass of all other classes.
• Reference variable of type Object can refer to an object of any other
class.
• As arrays are implemented as classes a variable of type Object can
also refer to any array.
Object defines the following methods, these are available in
every object:
Thank you!

You might also like