Java support multiple inheritance

Discussion in 'Programming' started by raghu1, Sep 15, 2011.

  1. #1
    Does java support multiple inheritance?
     
    raghu1, Sep 15, 2011 IP
  2. Vinil Mehta

    Vinil Mehta Member

    Messages:
    190
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #2
    java does support multiple inheritances via interface.we cant extend multiple classes but we can implement multiple interfaces :cool:
     
    Vinil Mehta, Sep 16, 2011 IP
  3. Vinil Mehta

    Vinil Mehta Member

    Messages:
    190
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #3
    multiple inheritance via interfaces is possible in java.We can inherit multiple interfaces but cant inherit multiple classess
     
    Vinil Mehta, Sep 18, 2011 IP
  4. amherstsowell

    amherstsowell Peon

    Messages:
    261
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Java does not support multiple inheritance, but by using threads we can use multiple inheritance.Take a look of example

    Single Inheritance Example

    class A {
    int x;
    int y;
    int get(int p, int q){
    x=p; y=q; return(0);
    }
    void Show(){
    System.out.println(x);
    }
    }
    class B extends A{
    void Showb(){
    System.out.println("B");
    }
    }

    class C extends B{
    void display(){
    System.out.println("C");
    }
    public static void main(String args[]){
    A a = new A();
    a.get(5,6);
    a.Show();
    }
    }



    Multiple Inheritance Example

    class A{
    int a;
    float b;
    void Show(){
    System.out.println("b in super class: " + b);
    }

    }

    class B extends A{
    int a;
    float b;
    B( int p, float q){
    a = p;
    super.b = q;
    }
    void Show(){
    super.Show();
    System.out.println("b in super class: " + super.b);
    System.out.println("a in sub class: " + a);
    }

    public static void main(String[] args){
    B subobj = new B(1, 5);
    subobj.Show();
    }
    }




    Output:
    C:\>java B
    b in super class: 5.0
    b in super class: 5.0
    a in sub class: 1
     
    amherstsowell, Sep 20, 2011 IP
  5. Vinil Mehta

    Vinil Mehta Member

    Messages:
    190
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #5
    How this is an example of multiple inheritance?
     
    Vinil Mehta, Sep 21, 2011 IP