0% found this document useful (0 votes)
15 views5 pages

Inheritance Notes With Output

Inheritance in Java is the process of acquiring properties and behaviors from one class to another, promoting code reusability and establishing IS-A relationships. There are various types of inheritance including single, multi-level, hierarchical, and multiple inheritance through interfaces. The super keyword allows child classes to access parent members, and method overriding enables runtime polymorphism.

Uploaded by

abrarhabib75
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)
15 views5 pages

Inheritance Notes With Output

Inheritance in Java is the process of acquiring properties and behaviors from one class to another, promoting code reusability and establishing IS-A relationships. There are various types of inheritance including single, multi-level, hierarchical, and multiple inheritance through interfaces. The super keyword allows child classes to access parent members, and method overriding enables runtime polymorphism.

Uploaded by

abrarhabib75
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/ 5

Inheritance in Java

Definition:
Inheritance is the process of acquiring properties (fields) and behaviors (methods) of one class into
another.
- Parent (super/base) class → The class whose properties are inherited.
- Child (sub/derived) class → The class that inherits the parent’s properties.
- Keyword: extends

Advantages:
- Code reusability
- IS-A relationship (Dog is-a Animal)
- Supports polymorphism (method overriding)

--------------------------------------------------
1) Single Level Inheritance

class Animal {
void eat() {
System.out.println("Animal is eating");
}
}

class Dog extends Animal {


void bark() {
System.out.println("Dog is barking");
}
}

public class SingleInheritanceDemo {


public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}

Output:
Animal is eating
Dog is barking

--------------------------------------------------
2) Multi Level Inheritance

class Animal {
void eat() {
System.out.println("Animal is eating");
}
}

class Dog extends Animal {


void bark() {
System.out.println("Dog is barking");
}
}

class BabyDog extends Dog {


void weep() {
System.out.println("Baby dog is weeping");
}
}

public class MultiLevelDemo {


public static void main(String[] args) {
BabyDog bd = new BabyDog();
bd.eat();
bd.bark();
bd.weep();
}
}

Output:
Animal is eating
Dog is barking
Baby dog is weeping

--------------------------------------------------
3) Hierarchical Inheritance

class Animal {
void eat() {
System.out.println("Animal is eating");
}
}

class Dog extends Animal {


void bark() {
System.out.println("Dog is barking");
}
}

class Cat extends Animal {


void meow() {
System.out.println("Cat is meowing");
}
}

public class HierarchicalDemo {


public static void main(String[] args) {
Dog d = new Dog();
Cat c = new Cat();
d.eat();
d.bark();
c.eat();
c.meow();
}
}

Output:
Animal is eating
Dog is barking
Animal is eating
Cat is meowing

--------------------------------------------------
4) Multiple Inheritance (Using Interfaces)

interface A {
void methodA();
}

interface B {
void methodB();
}

class C implements A, B {
public void methodA() {
System.out.println("Method A");
}
public void methodB() {
System.out.println("Method B");
}
}

public class MultipleInheritanceDemo {


public static void main(String[] args) {
C obj = new C();
obj.methodA();
obj.methodB();
}
}

Output:
Method A
Method B

--------------------------------------------------
Super Keyword Example

class Parent {
int x = 10;
Parent() {
System.out.println("Parent constructor");
}
void display() {
System.out.println("Parent method");
}
}

class Child extends Parent {


int x = 20;
Child() {
super(); // calls parent constructor
System.out.println("Child constructor");
}
void show() {
super.display(); // parent method
System.out.println("Parent x = " + super.x);
System.out.println("Child x = " + x);
}
}

public class SuperDemo {


public static void main(String[] args) {
Child c = new Child();
c.show();
}
}

Output:
Parent constructor
Child constructor
Parent method
Parent x = 10
Child x = 20

--------------------------------------------------
Method Overriding

class Father {
void marriage() {
System.out.println("You will marry XYZ girl");
}
}

class Son extends Father {


@Override
void marriage() {
System.out.println("I will marry ABC girl");
}
}

public class OverridingDemo {


public static void main(String[] args) {
Father f = new Son();
f.marriage();
}
}

Output:
I will marry ABC girl

--------------------------------------------------
Summary:
- Inheritance allows code reuse and establishes IS-A relationship.
- Types: Single, Multi-level, Hierarchical, Multiple (via interfaces), Hybrid.
- super keyword lets child access parent members.
- Method overriding provides runtime polymorphism.

You might also like