Chapter 7 - Inheritance in Java
Chapter 7 - Inheritance in Java
Inheritance in Java
1.3 Casting.............................................................................................................. 11
Chapter 7
Inheritance in Java
1
Page 1
7 INHERITANCE IN JAVA
Documentation Conventions
The following conventions are used in this guide:
Recall learning
Case study
Class session
Activity
Quiz
Reference
2
Page 2
7 INHERITANCE IN JAVA
Chapter 7: Inheritance in Java
Inheritance is one of the important concepts of object-oriented programming, as it allows
the creation of hierarchical classifications. By using inheritance, we can create a general
class that inherits the other. This chapter deals with inheritance concepts, Polymorphism,
overriding & the object classes.
3
Page 3
7 INHERITANCE IN JAVA
1. Inheritance in Java
1.1 Inheritance
Inheritance is the process of creating new classes from the existing classes. The new classes
are called derived classes & the existing classes are called base classes.
The derived classes inherit all the properties of the base classes plus its own properties. The
process of inheritance does not affect the base classes. The figure given below shows the
process of inheritance.
Property1
Property2
Property3
Property4 Base class
Inheritance
Property1
Property2
Inherits from
Base class
Property3
Derived class
4
Page 4
7 INHERITANCE IN JAVA
Advantages
- New classes can be derived by the user from the existing classes without any
modification
- It saves time & money
- It reduces program coding time
- It increases the reliability of the program.
1.2 Inheritance in java
Java supports the following types of inheritance. Java uses the extends keyword, to
set the relationship between a parent class and a child class. The following are the different
types of inheritance available in java. They are
- Single inheritance
- Multilevel inheritance
- Hierarchical inheritance
i) Single inheritance
A class derived from a super class is called single inheritance. The figure given below shows
the single inheritance.
5
Page 5
7 INHERITANCE IN JAVA
Here a subclass called staff is derived from a superclass student. The sub class staff contains
all the properties (student no, student name) of the superclass employee plus its own
properties (department, code, class code).
A class derived from an existing class is called subclass. The general form is
Where
Class.extends- keyword
Name1 –name of the subclass to be derived
Name2 – name of the class already existing
6
Page 6
7 INHERITANCE IN JAVA
Example Program
class A
{
int a,b;
A()
{
a=10;
b=25;
}
};
class B extends A
{
int c;
void show()
{
c=a+b;
System.out.println(c);
}}
class Const
{
public static void main(String args[])
{
B x1=new B();
x1.show();
}}
7
Page 7
7 INHERITANCE IN JAVA
Output
Multilevel inheritance
A class which is derived from other derived class is called multilevel inheritance. The figure
given below shows the process of multilevel inheritance.
8
Page 8
7 INHERITANCE IN JAVA
In the above example a subclass called staff is derived from a super class employee. This sub
class inherits all the properties of the super class employee(empno & emp name) plus its
own properties( deptname, total work and class). From this subclass another subclass called
NSS officer is derived. This class contains all the properties of teacher ( that is properties of
employee and teacher) plus its own process(NSS unit no and total strength.)
Example program
class A {
int i=10,j=20;
void xyz()
{
System.out.println(i+j);
}
void lmn()
{
System.out.println("lmn");
}}
class B extends A{
int k=40,l=45;
void abc() {
System.out.println(i+j+k);
lmn(); }}
class C extends B{ }
class Inherit{
public static void main(String args[]) {
C a1= new C();
a1.abc();}}
9
Page 9
7 INHERITANCE IN JAVA
Output
Hierarchical Inheritance
More classes derived from one super class is called hierarchical inheritance. The figure given
below shows the hierarchical inheritance.
In the above example two sub classes namely teaching and non-teaching are derived from a
super class employee. The subclasses contain all the properties of super class employee plus
its own properties.
The base class is treated as level1 class. From this class we can derive new classes that are
treated as level2 classes. From level2 classes we can derive new classes and are treated as
level3 classes and so on.
10
Page 10
7 INHERITANCE IN JAVA
1.3 Casting
The process of converting one data type in to another is called casting. There are two types
of conversion. They are
If one data type variable is assigned to another data type variable, the lower datatype
variable is automatically converted to higher data type before assigning. This process of
conversion is called automatic type conversion.
Eg:
float a;
int b=20;
a=b;
The process of converting one data type variable in to another locally, without affecting its
data type in the declaration is called explicit conversion. The general form is
(datatype)variable;
Where
11
Page 11
7 INHERITANCE IN JAVA
Variable - valid user defined name
Eg:
x=(int)5.25;
5.25 is converted to 5
float=a;
int b=15;
a=(float)b;
Overriding is a process of redefining the already defined method in a super class with all its
arguments in the subclass.
If an already defined method in a super class is redefined with all arguments in its sub class,
then the redefined method overrides the method in the super class. This means the sub
class method is active and the super class method is hidden.
If we want to call the method in the super class, we have to follow the syntax given below.
12
Page 12
7 INHERITANCE IN JAVA
Example program
class One {
int a,b;
One(int x,int y){
a=x;
b=y;}
void print(){
System.out.println("a="+a);
System.out.println("b="+b);
}}
class Two extends One{
int c;
Two(int x,int y,int z){
super(x,y);
c=z;}
void print(String msg){
System.out.println(msg);}
void print(){
System.out.println("c="+c);}}
class Override {
public static void main(String args[]){
Two s1=new Two(10,20,35);
s1.print("the numbers");
s1.print();
}}
Output
13
Page 13
7 INHERITANCE IN JAVA
………
Where
Final -keyword
Datatype, return type-valid datatype such as int, float etc.
varname, function name-valid user defined name
14
Page 14
7 INHERITANCE IN JAVA
1.5 Polymorphism
The word polymorphism means many forms. ”poly” means “many”. Polymorphism is a
technique used to write more than one function with same function name. It is the ability of
the object to take many forms. The functions may be in the same class or in different
derived classes.
15
Page 15
7 INHERITANCE IN JAVA
Example program
class Labour {
public void work() {
System.out.println("I am labour");
}
}
class Head extends Labour {
public void work() {
System.out.println("I am a head.");
}
Output
16
Page 16
7 INHERITANCE IN JAVA
1.6 Super
While writing programs using inheritance, we normally don’t create objects for super class
.But we create objects only for the subclasses. This is because all the properties except the
constructors of the super class are inherited in the subclass. These can be accessed using the
sub class objects. But super class constructor cannot be accessed.
Super is a keyword used to call the super class constructor from sub class constructor. The
general form is
super(argument list);
where,
super- keyword
Rules to be followed
ii) Super() must be the first statement executed inside a subclass constructor
iii) The arguments in the super() must match the arguments in the constructor defined
in the superclass.
17
Page 17
7 INHERITANCE IN JAVA
Example Program
class Super1
{
int a,b;
Super1()
{
a=10;
b=20;
}}
class Sub extends Super1
{
int a;
void show()
{
a=super.a+b;
System.out.println(a);
}}
class Two
{
public static void main(String args[])
{
Sub sub1=new Sub();
sub1.show();
}}
Output
18
Page 18
7 INHERITANCE IN JAVA
1.7 The Object Class
The object class stands at the top of the class hierarchy. Every class in the Java is a
descendent of the Object class. The Object class defines the basic state and behavior that all
objects must have, such as the ability to compare oneself to another object, to convert to a
string, to wait on a condition variable, to notify other objects that a condition variable has
changed, and to return the object's class. Every class has Object as a super class.
To compare equality of two strings equals() method is used. It returns true if the method is
true & false otherwise.
The tostring method returns a string representation of the object.We can display a string
representation of a current thread in the following way,
System.out.println(Thread.currentThread().toString());
19
Page 19
7 INHERITANCE IN JAVA
equals() wait()
tostring() hashCode()
clone() getclass()
notify() finalize()
getname()
Exercise
2. Write a program to create vehicle class. Inherit the classes road vehicle, water vehicle
from vehicle class.
a) tostring()
b) getname()
c) wait()
Summary:
From the above chapter we are familiar with the following concepts
Inheritance
Casting methods
Method Overriding
Polymorphism
Object classes in java
20
Page 20