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

Practical No19

The document discusses implementing multiple inheritance in Java using both interfaces and abstract classes. It provides examples of a class extending two parent classes, implementing multiple interfaces, overriding default methods from multiple interfaces, and implementing abstract methods from multiple interfaces. The examples demonstrate that a class can implement more than one interface and override methods as needed to handle inheritance from multiple sources.

Uploaded by

Mamta Khartmol
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)
139 views5 pages

Practical No19

The document discusses implementing multiple inheritance in Java using both interfaces and abstract classes. It provides examples of a class extending two parent classes, implementing multiple interfaces, overriding default methods from multiple interfaces, and implementing abstract methods from multiple interfaces. The examples demonstrate that a class can implement more than one interface and override methods as needed to handle inheritance from multiple sources.

Uploaded by

Mamta Khartmol
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

Practical No-19 : Develop a program for implementation of multiple

inheritance

Multiple Inheritance without Interface


// First Parent class
class ParentClass1
{
void show()
{
System.out.println("ParentClass1");
}
}

// Second Parent Class


class ParentClass2
{
void show()
{
System.out.println("ParentClass2");
}
}

class SubClass extends ParentClass1, ParentClass2{


public static void main(String[] args) {
SubClass obj = new SubClass();

obj.show();
}
}

Output:
SubClass.java:18: error: '{' expected
class SubClass extends ParentClass1, ParentClass2{
^
1 error
Develop a program for implementation of multiple inheritance

interface Writeable {
// abstract class
public void Write();
}
interface Readable {
// abstract class
public void read();
}

class Students implements Writeable,Readable {

public void Write(){


System.out.println("Students writes");
}
public void read(){
System.out.println("Students read");
}
public static void main(String[] args) {
Students s=new Students();
s.Write();
s.read();
}
Output:

Students writes
Students read
A class can implements more than one interfaces
interface Interface1
{
public void show();
}
interface Interface2
{
public void show();
}
class SubClass implements Interface1, Interface2
{
public void show()
{
System.out.println("A class can implements more than one interfaces");
}
public static void main(String args[]){
SubClass obj = new SubClass();
obj.show();
}
}

Output:

A class can implements more than one interfaces

interface A //First interface


{
default void text()
{
System.out.println("Hello");
}
}

interface B //Second interface


{
default void text()
{
System.out.println("What's your name?");
}
}

class C implements A,B
{

public void text() //Default method in interface is overriden


{
A.super.text();  //text() method from first interface is called
B.super.text();  //text() method from second interface is called
}
}
class Main
{
public static void main(String args[])
{
C obj = new C();
obj.text();
}
}

Output:
Hello
What’s your name?
interface A // Interface 1
{
public abstract void text1();  // Abstract method
}

interface B // Interface 2
{
public abstract void text2(); // Abstract method
}

class C implements A,B // Interface is implemented


{

// Abstract methods are overridden


public void text1() 
{
System.out.println("Hello");
}

public void text2()
{
System.out.println("What's your name?");
}
}
class Main
{
public static void main(String[] args)
{
C obj = new C(); // Create a C object
obj.text1();
obj.text2();
}
}

Output:
Hello
What’s your name?

You might also like