We may not have the course you’re looking for. If you enquire or give us a call on 01344203999 and speak to our training experts, we may still be able to help with your training requirements.
Training Outcomes Within Your Budget!
We ensure quality, budget-alignment, and timely delivery by our expert instructors.
Java is considered one of the finest programming languages worldwide for its simplicity, security and resilience. Among its several functions and mechanisms, Inheritance in Java is one of the most important concepts in the programming language.
Because of its compatibility and scalability, Java is used prominently around the world, with nearly 72% of people or organisations working with it in some measure, according to Statista.
In this blog, we will learn more about Inheritance in Java A Detailed Explanation of its Various Types
Table of Contents
1) The basics of Inheritance in Java
2) Key terms related to Java Inheritance
3) Types of Inheritance in Java
4) Applications of Java Inheritance
5) Disadvantages of Inheritance in Java
6) Conclusion
The basics of Inheritance in Java
Inheritance in Java is based on the concept of classes and objects. A class is a framework for creating objects, while an object is a unit that represents an abstract entity in a class. Parent class or superclass in Inheritance refers to a new class created based on an existing class. The new class is called the child class or subclass. The properties of the parent class are inherited by the child class, and the child class can also add new properties and behaviours of its own.
In Java, Inheritance is implemented using the "extends" keyword. To create a child class that inherits from a parent class, we simply use the "extends" keyword followed by the name of the parent class. Here is an example:
class Animal
{
public void eat()
{
System.out.println("The animal is eating.");
}
}
class Dog extends Animal
{
public void bark()
{
System.out.println("The dog is barking.");
}
}
public class Main
{
public static void main(String[] args)
{
Dog dog = new Dog();
dog.eat(); // The animal is eating.
dog.bark(); // The dog is barking.
}
}
In this example, the Dog class extends the Animal class, which means it inherits the eat() method from the Animal class. The Dog class also has its method, bark(). When we create a new instance of the Dog class and call its eat() and bark() methods, we see that the eat() method is inherited from the Animal class, and the bark() method is specific to the Dog class.
Key terms related to Java Inheritance
Here are some key terms you need to know before getting started with Java Inheritance:
1) Inheritance: Inheritance is a mainstay of OOP (Object-Oriented Programming) which facilitates a single class to adopt characteristics and mannerisms of another class.
2) Superclass/Base class/Parent class: The class inherited from is known as the superclass, base class, or parent class. It's this class that creates the set of common properties and behaviours that all its subclasses inherit.
3) Subclass/Derived class/Child class: A class that is inherited from the superclass is child class or subclass or derived class. Based on the arrangement and modification of the inherited properties and behaviours, the class adds new concepts to those existing in the superclass.
4) Extends keyword: The extends keyword can be utilised to define an Inheritance relationship which is shared by a subclass and a superclass. Placed after the class name, it consists of the superclass's name.
5) Super keyword: The super keyword is used to call the constructor or method of the supers' class from the sub class. It is usually employed to make use of superclass's attributes or methods that the subclass has redefined.
6) Access modifiers: Access modifier are words or keywords that are used to determine the level of accessibility of the members (fields and methods) of a class from other classes. Java has four access modifiers: open, hidden, private, and public.
7) Final keyword: Last but not least, the final keyword cannot be inherited from a class or its method. Ultimately every class cannot be subclassed and any final method is not supposed to be overridden.
8) Abstract Class: A class, which cannot be initiated and is meant to be subclassed by, is designated as an Abstract Class. It could contain methods that should be realised by the subclass or even by a class inheriting the subclass.
9) Interface: A collection of abstract methods that define a contract for the classes that implement it is called an Interface. A class can implement multiple Interfaces.
10) Multiple Inheritance: Multiple Inheritances are properties which allow inheriting from different classes. Unlike in Java, interfaces support multiple inherent classes but classes do not support multiple Inheritance.
Improve your JavaScript skills through our JavaScript for Beginners Course.
Types of Inheritance in Java
There are four main types of Java inheritance. Here are the types of inheritance in Java explained, along with examples:
Single Inheritance
Single Inheritance is the most straightforward type of Inheritance in Java, where a child class extends only one parent class.
For example:
class Animal
{
void eat()
{
System.out.println("eating");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking");
}
}
public class Main
{
public static void main(String args[])
{
Dog d = new Dog();
d.bark();
d.eat();
}
}
Here, the Dog class inherits the eat() method from the Animal class.
Multi-level Inheritance
Multi-level inheritance is a type of inheritance where a child class extends another child class, which in turn extends a parent class.
Here is an example:
class Animal
{
void eat()
{
System.out.println("eating");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking");
}
}
class Pug extends Dog
{
void play()
{
System.out.println("playing");
}
}
public class Main
{
public static void main(String args[])
{
Pug p = new Pug();
p.play();
p.bark();
p.eat();
}
}
Here, the Pug class inherits the bark() method from the Dog class, which in turn inherits the eat() method from the Animal class.
Hierarchical Inheritance
In Hierarchical Inheritance multiple child classes extend the same parent class.
Here is an example:
class Animal
{
void eat()
{
System.out.println("eating");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking");
}
}
class Cat extends Animal
{
void meow()
{
System.out.println("meowing");
}
}
public class Main
{
public static void main(String args[])
{
Dog d = new Dog();
d.bark();
d.eat();
Cat c = new Cat();
c.meow();
c.eat();
}
}
Here, the Dog and Cat classes inherit the eat() method from the Animal class.
Improve your Java efficiency through our Java Engineer Training.
Applications of Java Inheritance
Inheritance is a pivotal part of Object-Oriented Programming (OOPs) that allows one class to inherit the properties and methods of another class. Java Inheritance enables developers to create new classes built upon existing classes, thereby reusing existing code and reducing redundancy.
Here are some of the main applications of Java Inheritance:
1) Code reusability: Inheritance represents the reintroduction of the code of an existing class by the creation of new classes that possess the characteristics and methods of those classes. This sharing reduces the number of procedures that must be coded and maintained.
2) Polymorphism: In Java, polymorphism, which is achieved through Inheritance, enables classes to be derived from a common base class. Through polymorphism, we can treat objects of different classes the same way as we treat objects in the same class. This is especially relevant when designing code to operate with a variety of different types of objects.
3) Method overriding: In Java Inheritance, the procedure of rewriting methods and leaving original methods of the super class in the sub class is known as method overriding. This is particularly useful in the case where the child method uses the same signature as the parent method but with a different behaviour.
4) Specialisation: In addition to that, Java Inheritance is used for writing classes that can have specialisations containing the properties and methods of more general classes. This can be advantageous when there is a need to develop various objects while they operate with the same functionalities.
5) Abstraction: Java Inheritance allows programmers to construct a category of abstract classes which will define what methods each class must implement. Thus, it helps the developers to design a customised interface for the cluster of packed classes.
Conclusion
Java is and will remain one of the most used programming languages worldwide because of its constant evolution, simplicity and resilience to change. Inheritance in Java is one of the most important pillars of OOPs, a key concept that aspirants are expected to learn, understand and master. Mastering this key concept will take you one step closer to mastering Java.
Learn and master the important concept of GUI programming with Java Swing Development Training Sign up soon!
Frequently Asked Questions
Inheritance is used in Object-Oriented Programming to establish a hierarchy among classes, enabling the creation of new classes that inherit attributes and behaviors from existing ones, promoting code reuse and abstraction.
class ParentClass { // Parent class attributes and methods } class ChildClass extends ParentClass { // Child class inheriting from ParentClass // Additional attributes and methods can be defined here } |
The Knowledge Academy takes global learning to new heights, offering over 30,000 online courses across 490+ locations in 220 countries. This expansive reach ensures accessibility and convenience for learners worldwide.
Alongside our diverse Online Course Catalogue, encompassing 17 major categories, we go the extra mile by providing a plethora of free educational Online Resources like News updates, Blogs, videos, webinars, and interview questions. Tailoring learning experiences further, professionals can maximise value with customisable Course Bundles of TKA.
The Knowledge Academy’s Knowledge Pass, a prepaid voucher, adds another layer of flexibility, allowing course bookings over a 12-month period. Join us on a journey where education knows no bounds.
The Knowledge Academy offers various Java Courses, including JavaScript for Beginners Course, Hibernate Training and Java Programming Course. These courses cater to different skill levels, providing comprehensive insights into Decision Making Statements in Java.
Our Programming & DevOps Blogs cover a range of topics offering valuable resources, best practices, and industry insights. Whether you are a beginner or looking to advance your skills, The Knowledge Academy's diverse courses and informative blogs have you covered.
Upcoming Programming & DevOps Resources Batches & Dates
Date
Mon 20th Jan 2025
Mon 3rd Mar 2025
Mon 12th May 2025
Mon 14th Jul 2025
Mon 22nd Sep 2025
Mon 17th Nov 2025