On January 27th, this site will be read-only as we migrate to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 30th, when you will be able to use this site as normal.

    Forum Stats

  • 3,889,629 Users
  • 2,269,769 Discussions
  • 7,916,800 Comments

Discussions

Difference between Multiple Inheritance and Generaliztion

2608088
2608088 Member Posts: 3
edited Feb 8, 2014 2:09PM in New To Java

As what i understand, Generalization is also know as  Inheritance and Aggregation. They may or may not co-exist in order for a class to work. The subclass and the super class

and on the other hand, multiple inheritance is not allowed in Java, Can anyone please explain to me how these two differs.

Thanks in advance

2608088

Answers

  • TPD-Opitz
    TPD-Opitz Dipl.-Ing, Dipl.-Inf GermanyMember Posts: 2,465 Silver Trophy
    edited Feb 8, 2014 2:09PM
    51dfae5b-b298-49e8-af8f-58ab1c731cc7 wrote:
    
    As what i understand, Generalization is also know as  Inheritance and Aggregation.
    
    
    

    No.

    Generalization and Inheritance are almost the same thing viewed from opposite direction. A class inherits behavior and properties from its supper class while a supper class is a generalization to its subclasses which means it provides equal behavior common for all the subclasses. -> Human is a subclass of Mammal and Mammal is a super class of Human and Dog.

    Aggregation has nothing to do with either one. It means that an object of on class contains and uses objects of other classes. -> A Car is an aggregation of Engine, Chassis, Transmission, Wheel,...

    51dfae5b-b298-49e8-af8f-58ab1c731cc7 wrote:
    They may or may not co-exist in order for a class to work.
    
    
    

    Inherittance/Generalisation and Aggregation are concepts. They cannot "exist" in your program. You can use it or not.

    51dfae5b-b298-49e8-af8f-58ab1c731cc7 wrote:
    The subclass and the super class and on the other hand, multiple inheritance is not allowed in Java, [...]
    
    
    

    Multiple inheritance leads to problems: Imagine you have this structure:

    class A {
        void someMethod(){
           System.println("in A");
    }
    }
    class B extends A {
        void someMethod(){
           System.println("in B");
    }
    }
    
    class C extends A {
        void someMethod(){
           System.println("in C");
    }
    }
    
    class D extends B,C {
    }
    
    

    what result do you expect when calling

    new D().someMethod();
    
    

    ?

    Java solves this by not having multiple inheritance. Despite of this Java allows to implement multiple interfaces. In contrary to multiple inheritance there is no problem when different interfaces declare the same method signature.

    bye

    TPD

    2608088TPD-Opitz
This discussion has been closed.