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

Inheritance Java

The document provides examples of Java inheritance, including single, hierarchical, and multiple inheritance through interfaces. It illustrates how classes can inherit methods from parent classes and how interfaces can be used to achieve multiple inheritance. The examples demonstrate the use of method overriding and the structure of class relationships in Java.

Uploaded by

iamavp1234
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Inheritance Java

The document provides examples of Java inheritance, including single, hierarchical, and multiple inheritance through interfaces. It illustrates how classes can inherit methods from parent classes and how interfaces can be used to achieve multiple inheritance. The examples demonstrate the use of method overriding and the structure of class relationships in Java.

Uploaded by

iamavp1234
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

import java.io.

*;
import java.lang.*;
import java.util.*;

class One {
public void print_geek()
{
System.out.println("Geeks");
}
}

class Two extends One


{
public void print_for()
{ System.out.println("for"); }
}

public class Main {

public static void main(String[] args)


{
Two g = new Two();
g.print_geek();
g.print_for();
g.print_geek();
}
}

import java.io.*;

import java.lang.*;

import java.util.*;

class One {

public void print_geek()

{
System.out.println("Geeks");

class Two extends One

public void print_for()

{ System.out.println("for"); }

class Three extends Two {

public void print_geek()

System.out.println("Geeks");

public class Main {

public static void main(String[] args)

Three g = new Three();


g.print_geek();

g.print_for();

g.print_geek();

Output
Geeks
for
Geeks

Hierarchical Inheritance
In Hierarchical Inheritance, one class serves as a superclass (base class) for
more than one subclass. In the below image, class A serves as a base class for
the derived classes B, C, and D.

 Java
// Java program to illustrate the

// concept of Hierarchical inheritance

class A {

public void print_A()

{ System.out.println("Class A"); }

class B extends A {

public void print_B() { System.out.println("Class B"); }

class C extends A {

public void print_C() { System.out.println("Class C"); }

class D extends A {

public void print_D() { System.out.println("Class D"); }

}
// Driver Class

public class Test {

public static void main(String[] args)

B obj_B = new B();

obj_B.print_A();

obj_B.print_B();

C obj_C = new C();

obj_C.print_A();

obj_C.print_C();

D obj_D = new D();

obj_D.print_A();

obj_D.print_D();

Output
Class A
Class B
Class A
Class C
Class A
Class D

Multiple Inheritance (Through Interfaces)


In Multiple inheritances , one class can have more than one superclass and
inherit features from all parent classes. Please note that Java
does not support multiple inheritances with classes. In Java, we can achieve
multiple inheritances only through Interfaces. In the image below, Class C is
derived from interfaces A and B.

Multiple Inheritance

 Java

// Java program to illustrate the

// concept of Multiple inheritance


import java.io.*;

import java.lang.*;

import java.util.*;

interface One {

public void print_geek();

interface Two {

public void print_for();

interface Three extends One, Two {

public void print_geek();

class Child implements Three {

public void print_geek()

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

public void print_for() { System.out.println("for"); }

// Drived class

public class Main {

public static void main(String[] args)

Child c = new Child();

c.print_geek();

c.print_for();

c.print_geek();

Output
Geeks
for
Geeks
class Animal {

public void eat() {


System.out.println("I can eat");
}
}

class Dog extends Animal {

public void eat() {

super.eat();
System.out.println("I eat dog food");
}

public void bark() {


System.out.println("I can bark");
}
}

class Main {
public static void main(String[] args) {

Dog l1 = new Dog();

L1.eat();
L1.bark();
}
}

You might also like