0% found this document useful (0 votes)
3 views7 pages

Oop 3

Inheritance in Java allows one class to inherit properties and methods from another class, promoting code reusability and organization. It is a fundamental aspect of Object-Oriented Programming (OOP) that supports various types of inheritance such as single, multi-level, and hierarchical inheritance. Additionally, concepts like method overriding and overloading enhance the functionality and flexibility of Java programming.

Uploaded by

Hani Taye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Oop 3

Inheritance in Java allows one class to inherit properties and methods from another class, promoting code reusability and organization. It is a fundamental aspect of Object-Oriented Programming (OOP) that supports various types of inheritance such as single, multi-level, and hierarchical inheritance. Additionally, concepts like method overriding and overloading enhance the functionality and flexibility of Java programming.

Uploaded by

Hani Taye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Inheritance and Polymorphism

What is Inheritance in Java?


Inheritance is a critical feature in which one object acquires the properties of
the parent class. It is a crucial aspect of OOPs (Object Oriented programming
systems). Inheritance in Java is the core feature of object-oriented
programming. It facilitates a derived class to inherit the features from the
parent class, through this hierarchical process the classes share various
features, attributes, methods, etc.
Java, Inheritance is an important pillar of OOP(Object-Oriented Programming).
It is the mechanism in Java by which one class is allowed to inherit the
features(fields and methods) of another class. In Java, Inheritance means
creating new classes based on existing ones. A class that inherits from another
class can reuse the methods and fields of that class. In addition, you can add
new fields and methods to your current class as well.

As the name suggests, inheritance means inheriting properties. The


mechanism by which a class is allowed to derive the properties of a different
class is termed inheritance. With the concept of inheritance, the information
in a program can be organized hierarchically.

Inheritance is one of the critical features of OOPs (Object Oriented


Programming System), where a new class is created from an existing class
(parent or base class). In simple terms, the inheritance would mean passing
down the characteristics of parents(habits, looks, height) to their child.

The most useful contribution of types of inheritance in java would be the


reusability of a code. As discussed earlier that the derived class would inherit
the feature, methods, etc from the parent class so the codes present in the
parent class would also be inherited into the derived class, this is how
reusability would work.

In Java, the concept of inheritance allows the users to create classes and use
the properties of existing classes. A few important terminologies associated
with the concept are:

 Class: It is defined as a group of objects sharing common properties.


 Sub Class: Also known as a derived or extended class, this class inherits
the features from another class. Also along with the inherited fields and
methods, the class generated from the class can add fields and methods
of its own.
 Super Class: The superclass represents the class whose features are
inherited by a sub-class.
 Reusability: The technique of reusability allows the user to create a class
(a new one) having the fields or methods of an already existing class. It
allows reusing the code.
Importance of Java Inheritance

Implementation of inheritance in Java provides the following benefits:

 Inheritance minimizes the complexity of a code by minimizing duplicate


code. If the same code has to be used by another class, it can simply be
inherited from that class to its sub-class. Hence, the code is better
organized.
 The efficiency of execution of a code increases as the code is organized in
a simpler form.
 The concept of polymorphism can be used along with inheritance.
Why Do We Need Java Inheritance?
 Code Reusability: The code written in the Superclass is common to
all subclasses. Child classes can directly use the parent class code.
 Method Overriding: Method Overriding is achievable only through
Inheritance. It is one of the ways by which Java achieves Run Time
Polymorphism.
 Abstraction: The concept of abstract where we do not have to
provide all details is achieved through inheritance. Abstraction only
shows the functionality to the user.
Important Terminologies Used in Java Inheritance
 Class: Class is a set of objects which shares common characteristics/
behavior and common properties/ attributes. Class is not a real-world
entity. It is just a template or blueprint or prototype from which
objects are created.
 Super Class/Parent Class: The class whose features are inherited is
known as a superclass(or a base class or a parent class).
 Sub Class/Child Class: The class that inherits the other class is
known as a subclass(or a derived class, extended class, or child
class). The subclass can add its own fields and methods in addition to
the superclass fields and methods.
 Reusability: Inheritance supports the concept of “reusability”, i.e.
when we want to create a new class and there is already a class that
includes some of the code that we want, we can derive our new class
from the existing class. By doing this, we are reusing the fields and
methods of the existing class.
The keyword “extends” is used while inheriting a class. It defines that the
functionality of the superclass is being extended to the subclass.

The new class created is called a sub-class while the inherited class is termed
a parent class.

Types of Java Inheritance

The different types of inheritance are observed in Java:

1. Single level inheritance

As the name suggests, this type of inheritance occurs for only a single class.
Only one class is derived from the parent class. In this type of inheritance, the
properties are derived from a single parent class and not more than that. As
the properties are derived from only a single base class the reusability of a
code is facilitated along with the addition of new features

2. Multi-level Inheritance

The multi-level inheritance includes the involvement of at least two or more


than two classes. One class inherits the features from a parent class and the
newly created sub-class becomes the base class for another new class.

As the name suggests, in the multi-level inheritance the involvement of


multiple base classes is there. In the multilevel inheritance in
java, the inherited features are also from the multiple base classes as the
newly derived class from the parent class becomes the base class for another
newly derived class.

3. Hierarchical Inheritance

The type of inheritance where many subclasses inherit from one single class is
known as Hierarchical Inheritance.

Hierarchical Inheritance a combination of more than one type of inheritance.


It is different from the multilevel inheritance, as the multiple classes are being
derived from one superclass. These newly derived classes inherit the features,
methods, etc, from this one superclass. This process facilitates the reusability
of a code and dynamic polymorphism (method overriding).

Polymorphism
It is one of the basic feature of object-oriented programming paradigm which is
used to implement different operation with the same function

Java Method Overriding


Inheritance in java involves a relationship between parent and child classes.
Whenever both the classes contain methods with the same name and
arguments or parameters it is certain that one of the methods will override
the other method during execution. The method that will be executed
depends on the object.

If the child class object calls the method, the child class method will override
the parent class method. Otherwise, if the parent class object calls the
method, the parent class method will be executed.

Method overriding also helps in implementing runtime polymorphism in java.


Let’s take a simple example to understand how method overriding works in
java.

class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("eating bread...");}
}
Rules For Method Overriding
 The access modifier can only allow more access for the overridden
method.

 A final method does not support method overriding.

 A static method cannot be overridden.

 Private methods cannot be overridden.


 The return type of the overriding method must be the same.

 We can call the parent class method in the overriding method using
the super keyword.

 A constructor cannot be overridden because a child class and a parent


class cannot have the constructor with the same name.

Note: if we want to use or access the super class method we have to


create object or use super keyword

Program to show overriding using super keyword

class Parent {
void show(){
System.out.println("parent class method");
}
class Child extends Parent {
void show(){
super.show();
System.out.println("child class method");
}
public static void main(String args[]){
Parent ob = new Child();
ob.show();
}
}
Output: parent class method
child class method

Java Method Overloading


Method overloading allows the method to have the same name which differs on the
basis of arguments or the argument types. It can be related to compile-time
polymorphism.

class OverloadingExample{
int add(int a,int b){return a+b;}
int add(int a,int b,int c){return a+b+c;}
}
The main advantage of using method overloading in Java is that it gives us the
liberty to not define a function again and again for doing the same thing. In the
below example, the two methods are basically performing division, so we can have
different methods with the same name but with different parameters. It also helps
in compile-time polymorphism.
public class Div{
public int div(int a , int b){
return (a / b); }

public int div(int a , int b , int c){


return ((a + b ) / c); }

public static void main(String args[]){


Div ob = new Div();
ob.div(10 , 2);
ob.div(10, 2 , 3);
}
}
Output: 5
4

Program to overload three methods with the same name.

public class Add{


public int add(int a , int b){
return (a + b);
}
public int add(int a , int b , int c){
return (a + b + c) ;
}
public double add(double a , double b){
return (a + b);
}
public static void main( String args[]){
Add ob = new Add();
ob.add(15,25);
ob.add(15,25,35);
ob.add(11.5 , 22.5);
}
}
Output: 40
75
34

Their difference is clearly written in the below table

No Method Overloading Method Overriding


.

1) Method overloading is used to increase the Method overriding is used to


readability of the program. provide the specific
implementation of the method
that is already provided by its
super class.

2) Method overloading is performed within class. Method overriding occurs in two


classes that have IS-A
(inheritance) relationship.

3) In case of method overloading, parameter In case of method


must be different. overriding, parameter must be
same.

4) Method overloading is the example of compile Method overriding is the example


time polymorphism. of run time polymorphism.

5) In java, method overloading can't be Return type must be same or


performed by changing return type of the covariant in method overriding.
method only. Return type can be same or
different in method overloading. But you must
have to change the parameter.

You might also like