Professional Documents
Culture Documents
CHP 4 Inheritance
CHP 4 Inheritance
CHP 4 Inheritance
Inheritance
Inheritance in java
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a
parent object. It is an important part of OOPs (Object Oriented programming system).
The idea behind inheritance in Java is that you can create new classes that are built upon existing
classes. When you inherit from an existing class, you can reuse methods and fields of the parent
class. Moreover, you can add new methods and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
The syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
The extends keyword indicates that you are making a new class that derives from an existing class.
The meaning of "extends" is to increase the functionality.
1.Single Inheritance
1) Single Inheritance
Single inheritance is damn easy to understand. When a class
extends another one class only then we call it a single inheritance.
The below flow diagram shows that class B extends only one class
which is A. Here A is a parent class of B and B would be a child class
of A.
Multilevel Inheritance
2) Multilevel Inheritance
Multilevel inheritance refers to a mechanism in OO technology where one can
inherit from a derived class, thereby making this derived class the base class
for the new class. As you can see in below flow diagram C is subclass or child
class of B and B is a child class of A. For more details and example refer –
Multilevel inheritance in Java.
3.Multiple Inheritance
“Multiple Inheritance” refers to the concept of one class extending
(Or inherits) more than one base class. The inheritance we learnt
earlier had the concept of one base class or parent. The problem
with “multiple inheritance” is that the derived class will have to
manage the dependency on two base classes.
4.Hierarchical Inheritance
Hierarchical Inheritance
In such kind of inheritance one class is inherited by many sub classes. In below example
class B,C and D inherits the same class A. A is parent class (or base class) of B,C & D.
Read More at – Hierarchical Inheritance in java with example program.
5.Hybrid Inheritance
Hybrid Inheritance
In simple terms you can say that Hybrid inheritance is a combination of Single and
Multiple inheritance. A typical flow diagram would look like below. A hybrid inheritance can
be achieved in the java in a same way as multiple inheritance can be!! Using interfaces.
yes you heard it right. By using interfaces you can have multiple as well as hybrid
inheritance in Java.
class room
{
int l; Compilation and Execution:
int b;
room(int x, int y)
{ javac inheritance.java
l=x;
b=y; java inheritance
}
int area()
{
return(l*b); OUTPUT:
} area1=200
}
class bedroom extends room volume1=6000
{
int h;
bedroom(int x,int y, int z)
{
super(x,y);
h=z;
}
int volume()
{
return(l*b*h);
}
}
class inheritance
{
public static void main(String args[])
{
bedroom room1=new
bedroom(10,20,30);
int area1=room1.area();
int volume1=room1.volume();
System.out.println("area1="+area1);
System.out.println("volume1="+volume1);
}
}
super keyword
• The super keyword in java is a reference variable that
is used to refer parent class objects.
• The keyword “super” came into the picture with the
concept of Inheritance.
• It is majorly used in the following contexts:
➢ Use of super with variables:
➢ Use of super with methods:
Use of super with variables:
This scenario occurs when a derived class and base
class has same data members. In that case there is a
possibility of ambiguity for the JVM. We can
understand it more clearly using this code snippet:
Use of super with variables:
/* Base class vehicle */
class Vehicle
{
int maxSpeed = 120;
}
/* sub class Car extending vehicle */
class Car extends Vehicle
{
int maxSpeed = 180;
void display()
{
/* print maxSpeed of base class (vehicle) */
System.out.println("Maximum Speed: " + super.maxSpeed);
}
}
/* Driver program to test */
class Test
{
public static void main(String[] args) Maximum Speed:120
{
Car small = new Car();
small.display();
}
}
In the above example, both base class and subclass have a member maxSpeed. We
could access maxSpeed of base class in sublcass using super keyword.
Use of super with methods:
This is used when we want to call parent class method.
So whenever a parent and child class have same named
methods then to resolve ambiguity we use super
keyword. This code snippet helps to understand the said
usage of super keyword.
Use of super with methods:
/* Base class Person */
class Person
{
void message()
{
System.out.println("This is person class");
}
}
/* Subclass Student */
class Student extends Person
{
void message()
{
System.out.println("This is student class");
}
// Note that display() is only in Student class
void display()
{
message();
OUTPUT:
super.message();
} This is student class
} This is person class
/* Driver program to test */
class Test
{
public static void main(String args[])
{
Student s = new Student();
s.display();
}
}
Final Keyword In Java
The final keyword in java is used to restrict the user. The java
final keyword can be used in many context. Final can be:
• variable
• method
• class
The final keyword can be applied with the variables, a final
variable that have no value it is called blank final variable or
uninitialized final variable. It can be initialized in the constructor
only. The blank final variable can be static also which will be
initialized in the static block only. We will have detailed learning
of these. Let's first learn the basics of final keyword.
1) Java final variable
Abstraction in Java
Another way, it shows only essential things to the user and hides the internal
details, for example, sending SMS where you type the text and send the
message. You don't know the internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.
Abstract Class and Abstract Methods
interface Printable
{
void print();
}
interface Showable
{
void print();
}
Interfaces specify what a class must do and not how. It is the blueprint of the class.
An Interface is about capabilities like a Player may be an interface and any class
implementing Player must be able to (or must implement) move(). So it specifies a set of
methods that the class has to implement.
If a class implements an interface and does not provide method bodies for all functions
specified in the interface, then class must be declared abstract.