Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

Java supports Multiple Inheritance through Interface

Hello Friends,

I am Shailesh Patel doing MCA(5th Semester) from Ahmedabad. I am pretty impressed with the site and i would like to share my study knowledge to people who came across this site. This is my first post on this site. Give me feedback in comments. Thanks to Administrator (Rakshit Patel) to give me chance to share my knowledge through site.

May be you are worried why Java doesn't support multiple inheritance ? The reasons for omitting multiple inheritance from the Java language mostly step from the "simple, object oriented, and familiar" goal. As a simple language, Java's creators wanted a language that most developers could grasp without extensive training. To that end, they worked to make the language as similar to C++ as possible (familiar) without carrying over C++'s unnecessary complexity (simple).

Now look at below example , how the Java support Multiple Inheritance. Java supports multiple inheritance indirectly through the use of interface. In case we are able to extend more than one class then there would be a confusion of which method to process in case both classes have same method name(Same method signature also).

In case of using interfaces to support multiple inheritance we escape this problem as we define the methods that are needed only. Some important rules when we do use interface are…
1) All Method of Interface are Abstract
2) We don't write definition of Method in Interface
3) We don't create the object of Interface,We Must create Reference of Interface

Exemple :
interface One
{
//methods without definition
}
interface Two
{ //methods without definition
}
//Multiple Inheritance in java
class Final implements One,Two
{
One obj=New One();//Error(Object of Interface)
One obj=New Final();//Reference of Interface
//definition of all method
}

To know more about programming,JavaScript issues,jQuery,Expression Engine,MYSQL database,php info,php editor,programming php,Open-source,php help and php script , subscribe to our feed by entering email address below. You will get updates via email about every tutorial posted on this site . It will not take more than a sec.

Enter your email address:
 
Feel free to ask any question. Just leave your comment below and we will answer your comment with in 24 hours.
Share:

6 Comments

  • Nice explanation Shailesh. You have explain it in much better way than i thought. Keep it up..

  • Nice explanation Shailesh. You have explain it in much better way than i thought. Keep it up..

  • javastudent

    Hi Shailesh,
    i want to know why java dosn't support mutliple inheritence.
    i will explin by a code
    public class Main {            
        public static void main(String[] args) {        
              shape circleshape=new circle();          
                 circleshape.Draw();
        }}
    interface shape
    {     public   String baseclass="shape";     
         public void Draw();
     public void print();    
    }
    class circle implements shape
    {
        public void Draw() {
            System.out.println("Drawing Circle here");
        }
    public void print(){}    
    }
    this code got from by googling
    in the main class if i wrote 
    circle circleshape=new circle();          
                 circleshape.Draw();
    and
    class circle // implements shape (comment the imple)
    then i will get the wanted out put ,then why the fu..k interface (because every interviewr will ask about interface)is using we can easy call the same by circle circleshape=new circle();          
    OR the code change to
    public class Main {            
        public static void main(String[] args) {        
              shape circleshape=new circle();          
                 circleshape.Draw();    }
    }
    /*interface shape
    {
         public   String baseclass="shape";     
         public void Draw();
     public void print();    
    }*/
    class circle extends shape//implements shape
    {
        public void Draw() {
            System.out.println("Drawing Circle here);
        }
    public void print(){}    
    }
    class shape {
        public void Draw() {
            System.out.println("Drawing");
        }
    }
    and also we don't want to add the print() in the shape class as in interface
    Then what is mutiple inhertence means i dont want definition that is every where as a saying that "water water every where but not even a drop to drink".A eg:code which shows the problem in multiple interface and why that lead to interface (keyword) and then how it solved by interface,
    another thing is what is abstarct class the same for abstarct class also
    Thanks and regards
    javastudent

     

  • javastudent

    Hi Shailesh,
    i want to know why java dosn't support mutliple inheritence.
    i will explin by a code
    public class Main {            
        public static void main(String[] args) {        
              shape circleshape=new circle();          
                 circleshape.Draw();
        }}
    interface shape
    {     public   String baseclass="shape";     
         public void Draw();
     public void print();    
    }
    class circle implements shape
    {
        public void Draw() {
            System.out.println("Drawing Circle here");
        }
    public void print(){}    
    }
    this code got from by googling
    in the main class if i wrote 
    circle circleshape=new circle();          
                 circleshape.Draw();
    and
    class circle // implements shape (comment the imple)
    then i will get the wanted out put ,then why the fu..k interface (because every interviewr will ask about interface)is using we can easy call the same by circle circleshape=new circle();          
    OR the code change to
    public class Main {            
        public static void main(String[] args) {        
              shape circleshape=new circle();          
                 circleshape.Draw();    }
    }
    /*interface shape
    {
         public   String baseclass="shape";     
         public void Draw();
     public void print();    
    }*/
    class circle extends shape//implements shape
    {
        public void Draw() {
            System.out.println("Drawing Circle here);
        }
    public void print(){}    
    }
    class shape {
        public void Draw() {
            System.out.println("Drawing");
        }
    }
    and also we don't want to add the print() in the shape class as in interface
    Then what is mutiple inhertence means i dont want definition that is every where as a saying that "water water every where but not even a drop to drink".A eg:code which shows the problem in multiple interface and why that lead to interface (keyword) and then how it solved by interface,
    another thing is what is abstarct class the same for abstarct class also
    Thanks and regards
    javastudent

     

  • Mahascr

    Thank u very much….

Leave a Reply

Your email address will not be published.