Module3 Inheritance
Module3 Inheritance
Module 3
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
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;
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
{
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);
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
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()
{
}
}
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
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
}
}
{
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.