0% found this document useful (0 votes)
8 views3 pages

Inheritance Programs With Outputs

The document presents five Java inheritance programs demonstrating different types of inheritance: single level, multi level, hierarchical, use of the super keyword, and method overriding. Each program includes class definitions, constructors, and methods, followed by the expected output when executed. The examples illustrate how inheritance allows for code reuse and method overriding in object-oriented programming.

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)
8 views3 pages

Inheritance Programs With Outputs

The document presents five Java inheritance programs demonstrating different types of inheritance: single level, multi level, hierarchical, use of the super keyword, and method overriding. Each program includes class definitions, constructors, and methods, followed by the expected output when executed. The examples illustrate how inheritance allows for code reuse and method overriding in object-oriented programming.

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/ 3

Inheritance Programs with Outputs

Program 1: Single Level Inheritance

class Animal {
Animal() {
System.out.println("Parent class constructor");
}
void display() {
System.out.println("I am from Animal class");
}
}

class Dog extends Animal {


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

public class SLI_Demo {


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

Output:
Parent class constructor
Child class constructor
I am from Animal class
Dog is barking

Program 2: Multi Level Inheritance

class Animal {
Animal() {
System.out.println("Parent class constructor");
}
void display() {
System.out.println("I am from Animal class");
}
}

class Dog extends Animal {


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

class BabyDog extends Dog {


BabyDog() {
System.out.println("Baby Dog constructor");
}
void weep() {
System.out.println("I am Baby Dog");
}
}

public class MLI_Demo {


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

Output:
Parent class constructor
Child class constructor
Baby Dog constructor
I am from Animal class
Dog is barking
I am Baby Dog

Program 3: Hierarchical Inheritance

class Animal {
void display() {
System.out.println("This is Animal class");
}
}

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 HI_Demo {


public static void main(String[] args) {
Dog ob1 = new Dog();
Cat ob2 = new Cat();
ob1.display();
ob1.bark();
ob2.meow();
}
}

Output:
This is Animal class
Dog is barking
Cat is meowing

Program 4: Super Keyword

class Parent {
int m;
Parent(int z) {
m = z;
}
}

class Child extends Parent {


int m;
Child(int x, int y) {
super(x);
m = y;
}
void display() {
System.out.println("m of Child: " + m);
System.out.println("m of Parent: " + super.m);
}
}

public class Demo {


public static void main(String[] args) {
Child ob = new Child(100, 200);
ob.display();
}
}

Output:
m of Child: 200
m of Parent: 100

Program 5: Method Overriding

class Father {
void property() {
System.out.println("I will give you money");
}
void marriage() {
System.out.println("You marry XYZ girl");
}
}

class Son extends Father {


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

public class OverrideDemo {


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

Output:
I will give you money
I will marry ABC girl

You might also like