Multiple inheritance is Possible in Java?

Answer of above question is mention in below figure. Yes, Multiple inheritance possible in java using of interfaces.


Interface1                               Interface2

            |                                           |   [ Implements ] [Extends]

            ——————————

                                 |

                            Class

 


 

Inter1 in InterPack1 Package

package InterPack1;

public interface Inter1   // Interfaces

{

                public int x = 10;                // it’s final variable          

                public void method1();  // Indexing | Only Dec

}


Inter2 in InterPack2 Package

package InterPack2;

public interface Inter2   // Interfaces

{

                public void method2();  // Indexing | Only Dec

}


Class B in classStore Package

package classStore;

import InterPack1.*;

import InterPack2.*;

public class B implements Inter1 , Inter2                // Multiple inheritance

{

                public void method2()

                {

                                System.out.println(“Welcome in Inter2”);

                }

                public void method1()

                {

                                System.out.println(“Welcome in Inter1”);

                }

}


Class Example2 in mainPackage package

package mainPackage;

import classStore.*;

import InterPack1.*;

import InterPack2.*;

public class Example2

{

                public static void main(String[] args)

                {

                                Inter1 i1 = new B();

                                Inter2 i2 = new B();

                                i1.method1();    // from inter1

                                i2.method2();    // from inter2

                }

}

 

Output:

Welcome in Inter1

Welcome in Inter2

 

Video available on YouTube. : Multiple Inheritance 

 


[sbscrbr_form]

If you have any query on blog example, you can directly comment us or subscribe our website

 

Earn Money Online

1014total visits,3visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image