Module3 Inheritance

You might also like

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

ES05-Object Oriented Programming FE Sem II Mandar Sohani

Module 3
INHERITANCE

• Inheritance is one of the important feature of object oriented programming.


• The process deriving new class from existing class is called as inheritance.
• The existing class from which new class is derived is called as super class.
• The new class which may consists of additional specialized properties is called as
sub class.
• The existing class properties i.e. data members and member methods are acquired
by the new class.
• It facilitates reuse of already tested, debugged class methods.
• The keyword extends is used to inherit new class from super class.
• The general form of a class declaration that inherits a superclass is shown here:

class subclass-name extends superclass-name


{
// body of class
}
Types of inheritance:
1. Single inheritance:
If a single subclass is created from a superclass then it called as single inheritance.
2. Multilevel inheritance:
If a subclass is created from another subclass then it is called as multilevel
inheritance.
3. Hierarchical inheritance:
If more than one subclass are created from single super class then it is called as
hierarchical inheritance.
4. Hybrid Inheritance:
If any of the other types of inheritance are combined to create a sub class then it is
called as hybrid inheritance.

super keyword:
• super keyword has two general forms:
1. It can be used to call the super class constructor from sub class constructor.
A subclass can call a constructor method defined by its super class by use of the
following form of super:
super(parameter-list);
Here, parameter-list specifies any parameters needed by the constructor in the
super class.
super( ) must always be the first statement executed inside a subclass constructor.
super( ) always refers to the super class immediately above the calling class.
2. It can be used to access a member of the super class that has been hidden by a
member of a subclass.
This second form of super is when member names of a subclass hide members by
the same name in the super class.
1
Notes by – Mandar Sohani
CO3: Elaborate the concept of strings, arrays, and vectors.
CO4: Implement the concept of inheritance and interfaces.
ES05-Object Oriented Programming FE Sem II Mandar Sohani

This usage has the following general form:


super.member
Here, member can be either a superclass method or an instance variable.

Example: Program to implement the inheritance shown in diagram


class Staff
{

int code;
String name;
Staff(int c,String n)
{
code=c;
name=n;
}
void showdata()
{
System.out.println("Code = "+code);
System.out.println("Name = "+name);
}
}
class Teacher extends Staff
{
String subject;
String publication;

Teacher(int c,String n,String s,String p)


{
super(c,n);
subject=s;
publication=p;
}
void showdata()
{
super.showdata();
System.out.println("Subject = "+subject);
System.out.println("Publication = "+publication);
}
}
class Officer extends Staff
{
String grade;
Officer(int c,String n,String g)
{
super(c,n);
2
Notes by – Mandar Sohani
CO3: Elaborate the concept of strings, arrays, and vectors.
CO4: Implement the concept of inheritance and interfaces.
ES05-Object Oriented Programming FE Sem II Mandar Sohani

grade=g;
}
void showdata()
{
super.showdata();
System.out.println("Grade = "+grade);
}
}
class Typist extends Staff
{
int speed;
Typist(int c,String n,int s)
{
super(c,n);
speed=s;
}
void showdata()
{
super.showdata();
System.out.println("Speed = "+speed);
}
}
class Casual extends Typist
{
int dailywages;
Casual(int c,String n,int s,int dw)
{
super(c,n,s);
dailywages=dw;
}
void showdata()
{
super.showdata();
System.out.println("Dailywages = "+dailywages);
}
}
class Regular extends Typist
{
int pay;
Regular(int c,String n,int s,int p)
{
super(c,n,s);
pay=p;
}
void showdata()
3
Notes by – Mandar Sohani
CO3: Elaborate the concept of strings, arrays, and vectors.
CO4: Implement the concept of inheritance and interfaces.
ES05-Object Oriented Programming FE Sem II Mandar Sohani

{
super.showdata();
System.out.println("Monthly Pay = "+pay);
}
}
class EmployeeMain
{

public static void main(String args[])


{
Casual c=new Casual(1001,"Sam",40,100);
Officer o=new Officer(2001,"Tom","A");
Teacher t=new Teacher(3001,"Dick","Maths","TMH Pub.");
Regular r=new Regular(4001,"Harry",30,5000);
c.showdata();
o.showdata();
t.showdata();
r.showdata();
}
}

Abstract class:
Sometimes it is needed to define super class which consists of prototype of one or more
methods. The definition of these methods is given in the subclasses as per need. This
situation can occur when a super class is unable to create a meaningful implementation
for a method. It only defines a generalized form that will be shared by all of its
subclasses, leaving it to each subclass to fill in the details.
Such methods must be declared by using modifier abstract in the super class, and called
as abstract methods.
The general form:
abstract returntype methodname( parameters);

• If a class consists of one or more abstract methods then it must be declared


abstract, and called as abstract class.
• It can not be used to create the objects.
• It can be used to create the object reference.
• The subclass of abstract class must define all the methods which are declared
as abstract in the superclass otherwise it is also treated as abstract class.

final Keyword:
The final keyword is having three different uses in Java.
1. It can be used to create the variable whose value will remain always constant.
Example: final int a = 10;
4
Notes by – Mandar Sohani
CO3: Elaborate the concept of strings, arrays, and vectors.
CO4: Implement the concept of inheritance and interfaces.
ES05-Object Oriented Programming FE Sem II Mandar Sohani

2. It can be used to define a method which can not be redefined in the subclass.
Hence it is used to prevent overriding.
Example:
final void test()
{
System.out.println("This is a final method.");
}
This method is called as final method.
Since final methods cannot be overridden, a call to it can be resolved at compile
time.
Final methods are considered as inline methods.
3. It can be used to prevent the inheritance. The class declared by using final is
called as final class.
It can not be used as superclass.
The member methods of final class will be automatically treated as final methods.
final class Abc
{
//member methods are final
}
// The following class is illegal.
class Xyz extends Abc
{
// ERROR! Can't subclass Abc
}

INTERFACE:
• Interfaces as very much similar to class. They can be declared by using
keyword interface.
• By default the member methods of interface are abstract. They can only be
declared inside the interface.
• The variables declared inside the interface are by default final and static.They
must be initialized with some constant value.
• Interface can not be used to create the object.
• It can be used to create a subclass from it.
• The class derived from class is called as implementation class.
• The keyword implements is used to create subclass from interface.
• The implementation class must define all the methods which are declared in
the interface otherwise it is treated as abstract class.
• They are used to implement multiple interface inheritance.
• The interface can be defined as :
Access_specifier interface name
{
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
datatype final-varname1 = value;
5
Notes by – Mandar Sohani
CO3: Elaborate the concept of strings, arrays, and vectors.
CO4: Implement the concept of inheritance and interfaces.
ES05-Object Oriented Programming FE Sem II Mandar Sohani

datatype final-varname2 = value;


.
.
}

Example:
import java.io.*;
interface Matrix
{
final static int M=3,N=3;
void readmatrix();
void displaymatrix();
void addmatrix();
void multmatrix();
void transposematrix();
}
class Matman implements Matrix
{
int a[][] = new int[M][N];
int b[][] = new int[M][N];
int c[][] = new int[M][N];
public void readmatrix()
{
DataInputStream in = new DataInputStream(System.in);
Try
{
System.out.println("Enter matrix1 elements:");
for( int i = 0; i<M; i++)
{
for( int j = 0; j<N; j++)
{
a[i][j] = Integer.parseInt(in.readLine());
}
}
System.out.println("Enter matrix2 elements :");
for( int i = 0; i<M; i++)
{
for( int j = 0; j<N; j++)
{
b[i][j] = Integer.parseInt(in.readLine());
}
}
}
catch(Exception e)
{
6
Notes by – Mandar Sohani
CO3: Elaborate the concept of strings, arrays, and vectors.
CO4: Implement the concept of inheritance and interfaces.
ES05-Object Oriented Programming FE Sem II Mandar Sohani

System.out.println("IO ERROR");
}
}
public void addmatrix()
{

for( int i = 0; i<M; i++)


{
for( int j = 0; j<N; j++)
{
c[i][j] = a[i][j] +b[i][j];
}
}
}
public void multmatrix()
{
for( int i = 0; i<M; i++)
{
for( int j = 0; j<N; j++)
{
c[i][j] = 0;
for( int k = 0; k < N; k++)
{
c[i][j]+= a[i][k] * b[k][j];
}
}

}
}
public void transposematrix()
{
int t;
for( int i = 0; i<M; i++)
{
for( int j = 0; j<N; j++)
{
c[i][j]= a[j][i];
}
}
}
public void displaymatrix()
{
System.out.println("The resultant matrix is :");
for( int i = 0; i<M; i++)
{
7
Notes by – Mandar Sohani
CO3: Elaborate the concept of strings, arrays, and vectors.
CO4: Implement the concept of inheritance and interfaces.
ES05-Object Oriented Programming FE Sem II Mandar Sohani

for( int j = 0; j<N; j++)


{
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
class MatrixMain
{
public static void main(String args[])
{
Matman T = new Matman();
T.readmatrix();
T.addmatrix();
T.displaymatrix();
T.multmatrix();
T.displaymatrix();
T.transposematrix();
T.displaymatrix();
}
}

Overriding methods:
• The process of defining more than one methods, with same name and type
signature, one in superclass and other in subclasses is called as method
overriding.
• 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:
class Abc
{
int i, j;
Abc(int a, int b)
{
i = a;
j = b;
}
void show()
{
System.out.println("i = " + i + " j = " + j);
}
}
class Xyz extends Abc
8
Notes by – Mandar Sohani
CO3: Elaborate the concept of strings, arrays, and vectors.
CO4: Implement the concept of inheritance and interfaces.
ES05-Object Oriented Programming FE Sem II Mandar Sohani

{
int k;
Xyz(int a, int b, int c)
{
super(a, b);
k = c;
}
void show()
{
System.out.println("k: " + k);
}
}
class OverrideMethod
{
public static void main(String args[])
{
Xyz x = new Xyz(10, 20, 30);
x.show(); // this calls show() in B
}
}

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 is important because this is how Java implements run-
time polymorphism.
• When an overridden method is called through a superclass reference, Java
determines which version of that method to execute based upon the type of the
object being referred to at the time the call occurs.
• Therefore, if a superclass contains a method that is overridden by a subclass, then
when different types of objects are referred to through a superclass reference
variable, different versions of the method are executed.

RUN TIME POLYMORPHISM:


1. Function overriding is needed to implement run time polymorphism.
2. Run time polymorphism allows a general class to specify methods that will be
common to all of its derived classes, while allowing subclasses to define the
specific implementation of some or all of those methods.
3. The subclass object can be assigned to the base class reference.
4. A call to a function is resolved during runtime.
Example:
class Shape
{
int dim1,dim2;
void area()
9
Notes by – Mandar Sohani
CO3: Elaborate the concept of strings, arrays, and vectors.
CO4: Implement the concept of inheritance and interfaces.
ES05-Object Oriented Programming FE Sem II Mandar Sohani

{
System.out.println("Shape not defined area = 0");
}
}
class Rectangle extends Shape
{
Rectangle(int d1, int d2)
{
dim1 = d1;
dim2 = d2;
}
void area()
{
System.out.println("Area of rectangle="+(dim1 * dim2));
}
}
class Triangle extends Shape
{
Triangle(int d1, int d2)
{
dim1 = d1;
dim2 = d2;
}
void area()
{
System.out.println("Area of triangle="+(0.5*dim1 * dim2));
}
}
class OverMain
{
public static void main(String args[])
{
Shape s = new Shape();
Rectangle r = new Rectangle(3,4);
Triangle t = new Triangle(5,10);
s.area();
s = r;
s.area();
s = t;
s.area();
}
}

10
Notes by – Mandar Sohani
CO3: Elaborate the concept of strings, arrays, and vectors.
CO4: Implement the concept of inheritance and interfaces.

You might also like