You are on page 1of 16

• 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 we 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.

Why use inheritance in java

• For Method Overriding (so runtime polymorphism can be achieved).

• For Code Reusability.

• terms used in Inheritance

• 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.

• Super Class/Parent Class: Superclass is the class from where a subclass inherits the features.
It is also called a base class or a parent class.

• Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse
the fields and methods of the existing class when you create a new class. You can use the
same fields and methods already defined in the previous class.

Parent class
Superclass
Base class

Child class
Sub class
Derived class
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.

• In the terminology of Java, a class which is inherited is called a parent or superclass,


and the new class is called child or subclass.

Types of inheritance in java

• On the basis of class, there can be three types of inheritance in java: single, multilevel and
hierarchical.

• In java programming, multiple and hybrid inheritance is supported through interface only.
We will learn about interfaces later.
Single inheritance
When a class inherits another class, it is known as a single
inheritance. In the example given below, Dog class inherits the
Animal class, so there is the single inheritance
class Animal
{
void eats()
{
System.out.println(“animal eats…..”);
}
Class Dog extends Animal
{
Void sounds()
{
System.out.println(“Dog barks……”);
}
Public static void main(String args[])
{
Dog d=new Dog();
d.eats();
d.barks();
}
}
Output:
Animal eats….
Dog barks….
Multilevel Inheritance Example
• Multilevel Inheritance Example
When there is a chain of inheritance, it is known as multilevel
inheritance

class A

class B

class C
class A
{
void show()
{
System.out.println(“A class show method");
}
class B extends A
{
void fun()
{
System.out.println(“B class fun method");
}
class C extends B
{
void m1()
{
System.out.println(“C class m1() method");
}
public static void main(String args[])
{
C c=new C();
c.show();
c.fun();
c.m1();
B b=new B();
b.show();
b.fun();
A a=new A();
a.show();
}
}
output:
A class show method
B class fun method
C class m1() method
A class show method
B class fun method
A class show method

3. Hierarchical Inheritance: In Hierarchical Inheritance, one class


serves as a superclass (base class) for more than one subclass. In
the below image, class A serves as a base class for the derived class
B, C and D.
class Parent
{
void m1()
{
System.out.println(“Parent class m1() method");
}
class Child extends Parent
{
void m2()
{
System.out.println(“child class m2() method");
}
class Child1 extends Parent
{
void m3()
{
System.out.println(“child1 class m3() method");
}
public static void main(String args[])
{
Child c=new Child();
c.m1();
c.m2();
Child1 c1=new Child1();
c1.m1();
c1.m3();
}
}
Output:
Parent class m1() method
Child class m2() method
Parent class m1() method
Child1 class m3() method

person

student Non-teaching
student Staff staff
Polymorphism in Java
• The word polymorphism means having many forms. In simple
words, we can define polymorphism as the ability of a message
to be displayed in more than one form.
• Real life example of polymorphism: A person at the same time
can have different characteristic. Like a man at the same time is
a father, a husband, an employee. So the same person posses
different behavior in different situations. This is called
polymorphism.
• Polymorphism is considered one of the important features of
Object-Oriented Programming. Polymorphism allows us to
perform a single action in different ways. In other words,
polymorphism allows you to define one interface and have
multiple implementations. The word “poly” means many and
“morphs” means forms, So it means many forms.
In Java polymorphism is mainly divided into two types:
• Compile time Polymorphism
• Runtime Polymorphism
• 1. Compile-time polymorphism: It is also known as static
polymorphism. This type of polymorphism is achieved by function
overloading or operator overloading. But Java doesn’t support
the Operator Overloading.

• Method Overloading: When there are multiple functions with


same name but different parameters then these functions are
said to be overloaded. Functions can be overloaded by change in
number of arguments or/and change in type of arguments.
class Overloading
{
static void sum()
{
int a=10,b=20;
int c=a+b;
System.out.println("sum= "+c);
}
static void sum(int a)
{
int b=70;
int c=a+b;
System.out.println("sum= "+c);
}
static void sum(int a,int b)
{
int c=a+b;
System.out.println("sum= "+c);
}
static void sum(int a,int b,int c)
{
int d=a+b+c;
System.out.println("sum= "+d);
}
public static void main(String args[])
{
sum();
sum(20);
sum(56,79);
sum(100,600,700);
}
}
Output:
Sum=30
Sum=90
Sum=135
Sum=1400

• 2. Runtime polymorphism: It is also known as Dynamic Method


Dispatch. It is a process in which a function call to the overridden
method is resolved at Runtime. This type of polymorphism is
achieved by Method Overriding.
• Method overriding, on the other hand, occurs when a derived
class has a definition for one of the member functions of the base
class. That base function is said to be overridden.
class MethodOverriding
{
int a=10;
final void m1()
{
System.out.println("a :"+a);
}
}
class ChildORding extends MethodOverriding
{
int a=100;
void m1()
{
System.out.println("a= "+ a);
}
public static void main(String args[])
{
ChildORding ch=new ChildORding();
ch.m1();
}
}
Output:
a:10
• Java Type Casting
• Type casting is when you assign a value of one primitive data
type to another type.
• In Java, there are two types of casting:
• Widening Casting (automatically) - converting a smaller type to
a larger type size
• byte -> short -> char -> int -> long -> float -> double
• Narrowing Casting (manually) - converting a larger type to a
smaller size type
• double -> float -> long -> int -> char -> short -> byte
• Widening Casting
• Widening casting is done automatically when passing a smaller
size type to a larger size type:
• Narrowing Casting
• Narrowing casting must be done manually by placing the type in
parentheses in front of the value:
• Typecasting is the assessment of the value of one primitive data
type to another type. In java, there are two types of casting
namely upcasting and downcsting as follows:
• Upcasting is casting a subtype to a super type in an upward
direction to the inheritance tree. It is an automatic procedure for
which there are no efforts poured in to do so where a sub-class
object is referred by a superclass reference variable. One can
relate it with dynamic polymorphism.
• Implicit casting means class typecasting done by the
compiler without cast syntax.
• Explicit casting means class typecasting done by the
programmer with cast syntax.
• Downcasting refers to the procedure when subclass type refers
to the object of the parent class is known as downcasting. If it is
performed directly compiler gives an error
as ClassCastException is thrown at runtime. It is only achievable
with the use of instanceof operator The object which is already
upcast, that object only can be performed downcast.
Final Keyword In Java
• Final variable
• Final method
• Final class
• If you make any variable as final, you cannot change the value of
final variable(It will be constant).
• If you make any method as final, you cannot override it.
• If you make any class as final, you cannot extend it.
Abstraction in Java
• Abstraction is a process of hiding the implementation details and
showing only functionality to the user.
• 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.
• Ways to achieve Abstraction
• There are two ways to achieve abstraction in java
• Abstract class (0 to 100%)
• Interface (100%)
• A class which is declared as abstract is known as an abstract
class. It can have abstract and non-abstract methods. It needs to
be extended and its method implemented. It cannot be
instantiated.
• Points to Remember
• An abstract class must be declared with an abstract keyword.
• It can have abstract and non-abstract methods.
• It cannot be instantiated.
• It can have constructors and static methods also.
• It can have final methods which will force the subclass not to
change the body of the method.

You might also like