Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Multiple Inheritance in Java with Example

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.40k
    Comment on it

    Hello!!

    As we know that java does not support the multiple inheritance but java 8 provides a way by which we can use multiple inheritance. It provides a default method to do this job.

    In java 7, interface are used to declare the contracts which implement classes must be implemented. so it is not give any specific behavior to interface so that any class can inherit. Even a class can inherit many interface but it is not appropriate for multiple inheritance.

    But in Java 8, It provide a default method which says that "if a class implement two interfaces and both defines default methods, then it is essentially inheriting behaviors from two parents which is multiple inheritance". 

    For example:-  

    package com.multipleInheritance.examples;
     
    interface Move
    {
        default void moveFast(){
            System.out.println("I am moving fast, buddy !!");
        }
    }
      
    interface Crawl
    {
        default void crawl(){
            System.out.println("I am crawling !!");
        }
    }
      
    public class Animal implements Move, Crawl 
    {
        public static void main(String[] args) 
        {
            Animal self = new Animal();
             
            self.moveFast();
            self.crawl();
        }
    }

    In the above example, Animal class have no behavior but it is inheriting it from the other.

    Possible conflicts and their resolutions:-  In the above example have two different interface and its method so that here is no conflicts but what happens if both of the interface have same name method? Here is a conflict occurs when we call the method from the Animal class, which method will invoke at that time. So here is the conflict.

    package com.multipleInheritance.examples;
     
    interface Move
    {
        default void run(){
            System.out.println("I am running, kid !!");
        }
    }
      
    interface Crawl
    {
        default void run(){
            System.out.println("I am running, daddy !!");
        }
    }
      
    public class Animal implements Move, Crawl 
    {
        public static void main(String[] args) 
        {
            Animal self = new Animal();
            //self.run();
        }
    }

    What will happen if self.run() will execute?

    For solving the above problem we will use the reference of the interface.

    Move.super.run(); 

    Or

    Crawl.super.run(); 

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: