Professional Documents
Culture Documents
When we inherit from an existing class, we can reuse methods and fields of
the parent class. Moreover, we can add new methods and fields in your
current class also.
• Class: A class
is a group of objects which have common properties. It is a
template or blueprint from which objects are created.
• Sub Class/Child Class: Subclass is a class which inherits the other class. It
is also called a derived class, extended class, or child class.
HARSH (04324302019)
The syntax Of Java Inheritance:
class Subclass-name extends Superclass-name
{
//methods and fields
}
Look at the example given below. Here Vehicle is the superclass and Sports is the
subclass . The relationship between the two classes is Sports IS-A Vehicle. It means
that Sports is a type of Vehicle.
HARSH (04324302019)
Single Inheritance
The example discussed above was an example of single inheritance.
Output:-
Multilevel in heritance
When there is a chain of inheritance, it is known as multilevel inheritance.
Output:-
Hierarchical Inheritance
In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one
subclass.
HARSH (04324302019)
Output:-
Interface?
An interface in Java is a blueprint of a class. It has static constants and abstract
methods.
interface <interface_name>{
// declare constant fields
// declare methods that abstract
// by default.
}
HARSH (04324302019)
Multiple Inheritance
In Multiple inheritances, one class can have more than one superclass and inherit features from
all parent classes. Java does not support multiple inheritances with classes. In java, we can
achieve multiple inheritances only through Interfaces.
Output:-
Abstract class
Abstraction is a process of hiding the implementation details and showing only
functionality to the user.
A class which is declared with the abstract keyword is known as an abstract class in
Java. It can have abstract and non-abstract methods (method with the body).
Output :-
HARSH (04324302019)
Method Overloading
If a class has multiple methods having same name but different in parameters, it is
known as Method Overloading.
Output:-
Method overriding
If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding in Java.
HARSH (04324302019)
-> Base class method gets hidden.
Output:-
Use of “Super”
Usage of Java super Keyword
1. super can be used to refer immediate parent class instance variable.
2. super can be used to invoke immediate parent class method.
3. super() can be used to invoke immediate parent class constructor.
Output:-
Output:-
Output:-
HARSH (04324302019)