-2

Here this program seems to be using multiple inheritance. But Java does not support multiple inheritance, so how is this program working?

What is the reason behind this code to compile when the two classes Student and Professor inherits the base class Person?

class Person
{
    private String name;
    private int age;
    private int val;

    private int value(int sal)
    {
        return sal/100;
    }

    public Person(String name, int age, int sal)
    {
        this.name = name;
        this.age = age;
        val = value(sal);
    }
    public void valOverride(int val)
    {
        this.val = val;
    }   

    public String getName()
    {
        return name;
    }
    public int getAge()
    {
        return age;
    }
    public int getVal()
    {
        return val;
    }
}

class Student extends Person
{
    Student(String name, int age, int sal)
    {
        super(name,age,sal);
    }
}

class Prof extends Person
{
    Prof(String name, int age, int sal)
    {
        super(name,age,sal);
    }
}

public class AbsDemo
{
    public static void main(String[] args)
    {
        Student s = new Student("prasi", 21, 18000);
        Prof pr = new Prof("david",38,25000);
        System.out.println(pr.getVal());
        pr.valOverride(22);
        Person p = new Student("prasanna", 22, 12000);
        System.out.println(s.getName());
        System.out.println(p.getName());
        System.out.println(s.getVal());
        System.out.println(pr.getVal());
    }
}
3
  • Java "doesn't support multiple inheritance" but has interface :). Anyway i can't see an inheritance here in this code! Commented Nov 25, 2014 at 14:06
  • Multiple inheritance: C is an A and a B. Your case: Cis an A, Bis an A Commented Nov 25, 2014 at 14:08
  • It's called polymorphism in p.getName(), the others are simple class. Commented Nov 25, 2014 at 14:09

9 Answers 9

5

Java "doesn't support multiple inheritance" insofar as a class can only extend one other class.

There is nothing wrong with multiple child classes of one parent class.

Sign up to request clarification or add additional context in comments.

1 Comment

Right on. Multiple inheritance would be like Prof extends Person, AndSomethingElse and that's not possible in java.
3

enter image description here

If use a

Person extends Student,Prof{}

You have a multiple inheritance, that isn't possible in JAVA.

1 Comment

same of you @Jamsheer, a bit different :). I don't see yours, i was doing a draw when you post it! 8 Min to make a draw :(
2

Java supports "multiple inheritance" in the following situations:

  • Interfaces (you can have multiple interfaces in one class)
  • Sequential (ClassA extends ClassB which extends ClassC)
  • Multiple Classes can extend the same Class (ClassA and ClassB both extend ClassC)

However the concept of Java multiple inheritance in (as defined loosely in Wikipedia):

Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and features from more than one parent object or parent class.

(http://en.wikipedia.org/wiki/Multiple_inheritance)

So it means it that Java does NOT support multiple inheritance if you are trying to add them in the same class (Class A extends ClassB,ClassC).

Being that, that code in the question it is not using the concept of Multiple Inheritance. It is just Multiple Classes extending the same class.

2 Comments

You are describing 3 scenario's that aren't multiple inheritance. Interfaces aren't inheritance as they don't extend the current class with parent implementations or properties. Sequential inheritance is single inheritance. (you inherit from your parent class, whatever the parent class actually has). I think your 3rd point is correct though the OP was confused about extending a class multiple times and thought it was multiple inheritance.
I've corrected my use of multiple inheritance :)
1

This is not multiple inheritance. Multiple inheritance would be when one class extends 2 other classes not when 2 different classes extend the same class. You can sort of get around multiple inheritance by creating interfaces. Java classes can implement as many interfaces as they want but they can only extend one class.

Not multiple inheritance(supported):

class Student extends Person

class Prof extends Person

Multiple inheritance(not supported):

class Student extends Person, Teenager

Comments

1

there is no multiple inheritance in this....

multiple inheritance is where one class is extended from two or more classes

Comments

1

Multiple inheritance would be inheriting from more than one class e.g.

BaseA    BaseB
 ^         ^   
  \       /
   \     /
    \   /
  DerivedA

Your example is single inheritance, occurring twice from the same base class:

        BaseA
          ^  
        /  \
       /    \
      /      \
  DerivedA  DerivedB 

Comments

0

enter image description here

This is the flow digaram of your code .Here there is no multiple inheritence concept

Comments

0

Formerly put, multiple inheritance would look like this:

class SomePerson extends Student, Prof //invalid
{...}

Java does not allow this type of multiple inheritance (with classes) but it does allow multiple classes inheriting from the same class, like in your example.

1 Comment

Thanks for clearing my confusions.. now i am clear..
0

Yes Java doesn't support multiple inheritance. You cannot extend two classes like this

public class A extends B, C

Your code is some what like this

public class A
{
   public class B extends A
   {

   }

    public class C extends A
   {

   }
}

This is the concept of inner classes and this is not multiple inheritance.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.