0% found this document useful (0 votes)
5 views

inheritance_notes

Inheritance in Java allows the creation of a new class based on an existing class, enabling code reusability and the addition of new features. It supports various types such as single, multilevel, and hierarchical inheritance, while method overriding allows child classes to redefine parent methods. Key concepts include upcasting, runtime polymorphism, and the use of the 'extends' keyword for inheritance.

Uploaded by

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

inheritance_notes

Inheritance in Java allows the creation of a new class based on an existing class, enabling code reusability and the addition of new features. It supports various types such as single, multilevel, and hierarchical inheritance, while method overriding allows child classes to redefine parent methods. Key concepts include upcasting, runtime polymorphism, and the use of the 'extends' keyword for inheritance.

Uploaded by

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

INHERITANCE

The idea of inheritance means, already you have some engineered product, and you are
making a similar one and you want to add some extra features so whatever the work
you have done, you don’t have to do it again here, just take it is and add extra features.
 Inheritance means creating a new class based on existing class.
 Inheritance (Blood relation) – is a relationship (100%)
 It is one of the ways to reusability in java.
 It is a process of acquiring the property and behavior of parent (Base, Super) class
into a new class child class.
 In java we use extends keyword to inherit a class.
 Whenever inheritance is done all data and functions are accessible in child class.

ADVANTAGES:
class B
{
1. Reusability
2. To achieve run-time polymorphism. }
class C extends B
3. Data hiding {

4. Method overriding }

Parent class

Animal

-eat()
-walk()
-sleep()

Child class Child class


Dog Cat
-eat() -eat()
-walk() -walk()
-sleep() -sleep()

Types of Inheritance:
1. Single level Inheritance
2. Multi-level Inheritance
3. Multiple Inheritance
4. Hybrid Inheritance
5. Hierarchical or Network Inheritance
Single level 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.

public class Animal {


public void eat() {
System.out.println("Eating...");
}

public void sleep() {


System.out.println("Sleeping...");
}

public void walk() {


System.out.println("Walking...");
}
}

class Dog extends Animal {


public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.sleep();
dog.walk();
dog.noise(); // Additional feature in Dog class
}

public void noise() {


System.out.println("Bow Bow...");
}
}

Multilevel Inheritance
When there is a chain of inheritance, it is known as multilevel inheritance.
class A {
public void test1() {
System.out.println("1");
}
}
class B extends A{
public void test2(){
System.out.println("2");
}

}
class C extends B {
public void test3() {
System.out.println("3");
}
public static void main(String[] args) {
C obj = new C();
obj.test1();
obj.test2();
obj.test3();
}
}
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.

package Inheritance;
public class Animal {
public void eat() {
System.out.println("Eating...");
}
Parent Class

public void sleep() {


System.out.println("Sleeping...");
}

public void walk() {


System.out.println("Walking...");
}
}

package Inheritance;

public class Dog extends Animal{


public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
Child Class

dog.sleep();
dog.walk();
dog.noise(); // Added extra features in dog class
}
public void noise() {
System.out.println("Bow Bow..");
}
}

package Inheritance;

public class Cat extends Animal {


public static void main(String[] args) {
Cat cat = new Cat();
cat.eat();
Child Class

cat.sleep();
cat.walk();
cat.noise();
}
public void noise() {
System.out.println("Meow Meow...");
}
}
Multiple Inheritance
When one class inherits multiple classes, it is known as multiple inheritance.
Note: Multiple inheritance is not supported in Java through class because of *Diamond
problem.
*Diamond problem: when same method is inherited to child class twice resulting in confusion of
inheritance hierarchy.

Hybrid Inheritance

In java we have interface by which we achieve both multiple and


hybrid interface.
 In java by default every class performs inheritance.
Java.lang.Object
Object class is Superclass, every class is inside Object class.
Q. How memory is called in case of inheritance?
Ans: In case of inheritance all the data of parent class gets memory into the child class object.

 In java we can hold the child class object into reference variable of parent class. This concept is called
upcasting.
 When we upcast, we can only access the over-ridden methods but not all the methods of the child
class.

class parent
{
}
class Child extends Parent
{
}
Parent obj = new Child(); //upcasting

Method Overriding
Sometimes a child class may not be satisfied with parent method implementation then the child class
is allowed to redefine that method based on its requirement, such type of concept is called method
overriding.

method overriding is a technique in which child class adds a method that is already present in its
parent class and adds new functionality to this method. Then we say that the base class method is
overridden.
Rules for Java Method Overriding

1. The method must have the same name as in the parent class

2. The method must have the same parameter as in the parent class.

3. There must be an IS-A relationship (inheritance).

 When parent class and child class have same name of method with same signature (method
name, return type, and parameters). So, we can say that child class overrides the parent class
method.
 When method overriding is done, always preference goes to child class overridden
method.

Syntax: The subclass method is annotated with @Override, although the annotation is optional.

The @Override annotation:


 Indicates Overriding: Shows that a method in a subclass overrides a superclass method.

 Compile-Time Checking: Ensures the method actually overrides a superclass method or not (check
method signature of parent and child class), otherwise it generates a compile-time error.

 Improves Readability: Makes code clearer by marking overridden methods explicitly.

When to Use super:


 When Overriding Methods: Use super to call the superclass's method that you have overridden if you
need to include the functionality of the superclass method in the subclass method.
class Parent {

public void property() {


System.out.println("Land+Cash+Gold");
}

public void marry() {


System.out.println("Rajlaxmi");
}
}

class Child extends Parent {


@Override
public void marry() {
System.out.println("Katrina Kaif");
}

public void career() {


System.out.println("Software Engineer");
}
}

class Test {
public static void main(String[] args) {
Parent p = new Parent();
p.marry(); // Output: Rajlaxmi (calls Parent's marry() method)

// Accessing the overridden method


Child c = new Child();
c.marry(); // Output: Katrina Kaif (calls Child's marry() method, overriding Parent's method)
c.career(); // Output: Software Engineer (calls Child's career() method)

Parent p1 = new Child(); // Upcasting: p1 is a reference of type Parent, but refers to a Child object
p1.marry(); // Output: Katrina Kaif (due to runtime polymorphism, Child's marry() method is called)

// p1.career(); // Compilation error: The method career() is undefined for the type Parent

// Explanation: When we upcast, we can access overridden methods (like marry()),


// but not methods that are exclusive to the Child class (like career()).
}
}

Parent p1 = new Child();


p1.marry();

 Output: Katrina Kaif

 Explanation: we are calling marry () method on p1, p1 is type of Parent type, compiler is always going to
check in parent class the marry () method is there or not. In the parent class if marry () method is not
present, gives compile time error, if method is present then perfectly fine. Now at run time JVM is always
going to check in the child class is marry () method is overridden or not, if overridden, priorities go to
child class hence child class method called. (this concept is called runtime polymorphism)

 At runtime, Java uses the actual object type (Child) to decide which method to call. This is known as
dynamic method dispatch or runtime polymorphism.

Q. Why is method overriding called run-time polymorphism?

Ans: In the case of method overriding, the method call is resolved dynamically at runtime. Hence it is
called dynamic polymorphism.

Note: In java we cannot override static method.

 static to non-static (NO)


 non-static to static (NO)
 static to static (YES) But this is not overriding, this is method hiding
class Animal {
// Parent class method
public void eat() {
System.out.println("The animal is eating");
}
}

class Herbivore extends Animal {


// Child class overrides the parent class method
@Override
public void eat() {
System.out.println("The herbivore is eating plants");
}
}

class Carnivore extends Animal {


// Another child class overrides the parent class method
@Override
public void eat() {
System.out.println("The carnivore is eating meat");
}
}

public class Test {


public static void main(String[] args) {
Animal generalAnimal = new Animal(); // Parent class object
Animal herbivoreAnimal = new Herbivore(); // Herbivore object
Animal carnivoreAnimal = new Carnivore(); // Carnivore object

generalAnimal.eat(); // Calls the Animal's eat method


herbivoreAnimal.eat(); // Calls the Herbivore's eat method (overridden method)
carnivoreAnimal.eat(); // Calls the Carnivore's eat method (overridden method)
}
}

You might also like