0% found this document useful (0 votes)
0 views6 pages

03_Inheritance in Java

Java Inheritance is a core concept of Object-Oriented Programming that allows one class to inherit features from another, promoting code reusability and method overriding. It involves key terminologies such as superclass, subclass, and the 'extends' keyword, and can be categorized into single, multilevel, and hierarchical inheritance types. The advantages of inheritance include improved code organization, easier maintenance, and support for polymorphism.

Uploaded by

ug2302070
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)
0 views6 pages

03_Inheritance in Java

Java Inheritance is a core concept of Object-Oriented Programming that allows one class to inherit features from another, promoting code reusability and method overriding. It involves key terminologies such as superclass, subclass, and the 'extends' keyword, and can be categorized into single, multilevel, and hierarchical inheritance types. The advantages of inheritance include improved code organization, easier maintenance, and support for polymorphism.

Uploaded by

ug2302070
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/ 6

Inheritance in Java

Java Inheritance is a fundamental concept in 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.

Why Use Inheritance in Java?


 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. Run time polymorphism happens only through method
overriding, not method overloading.
 Abstraction: The concept of abstraction where we do not have to provide
all details, is achieved through inheritance. Abstraction only shows the
functionality to the user.
 that Organizes classes in a structured manner, improving readability and
maintainability.

Key Terminologies Used in Java Inheritance


 Class: Class is a set of objects that share 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.
 Extends Keyword: This keyword is used to inherit properties from a
superclass.
 Method Overriding: Redefining a superclass method in a subclass.
 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.

How Inheritance Works in Java?

The extends keyword is used for inheritance in Java. It enables the


subclass to inherit the fields and methods of the superclass. When a class
extends another class, it means it inherits all the non-primitive members
(fields and methods) of the parent class, and the subclass can also
override or add new functionality to them.
Note: The extends keyword establishes an "is-a" relationship between
the child class and the parent class. This allows a child class to have all
the behavior of the parent class.
Syntax:
The syntax for inheritance in Java is listed below:
class ChildClass extends ParentClass {
// Additional fields and methods
}

Types of Inheritance in Java


On the basis of class, there can be three types of inheritance in java: single,
multilevel and hierarchical.
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.

1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. public class Main{
8. public static void main(String args[]){
9. Dog d=new Dog();
10. d.bark();
11. d.eat();
12. }}

Multilevel Inheritance
When there is a chain of inheritance, it is known as multilevel inheritance. As you
can see in the example given below, BabyDog class inherits the Dog class which
again inherits the Animal class, so there is a multilevel inheritance.

1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class BabyDog extends Dog{
8. void weep(){System.out.println("weeping...");}
9. }
10. public class Main{
11. public static void main(String args[]){
12. BabyDog d=new BabyDog();
13. d.weep();
14. d.bark();
15. d.eat();
16. }}

Hierarchical Inheritance

When two or more classes inherits a single class, it is known as


hierarchical inheritance. In the example given below, Dog and Cat
classes inherits the Animal class, so there is hierarchical
inheritance.

1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class Cat extends Animal{
8. void meow(){System.out.println("meowing...");}
9. }
10. public class Main{
11. public static void main(String args[]){
12. Cat c=new Cat();
13. c.meow();
14. c.eat();
15. //c.bark();//C.T.Error
16. }}

Advantages of Inheritance
Inheritance offers several advantages, including:

Code Reusability: Inherited members from a superclass can be reused in


subclasses, reducing redundant code and promoting a modular approach to
software development.
Hierarchical Organization: Inheritance facilitates the creation of well-structured
class hierarchies, improving code readability and maintainability.
Polymorphism: Subclasses can override superclass methods, allowing for
polymorphic behavior, where methods can behave differently based on the object
type at runtime.
Easier Maintenance: Changes made to a superclass automatically propagate to its
subclasses, ensuring consistency and simplifying maintenance efforts.

You might also like