-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());
    }
}
Drew Kennedy
  • 4,118
  • 4
  • 24
  • 34
Prasanna Anbazhagan
  • 1,693
  • 1
  • 20
  • 37

9 Answers9

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.

Mena
  • 47,782
  • 11
  • 87
  • 106
  • Right on. Multiple inheritance would be like `Prof extends Person, AndSomethingElse` and that's not possible in java. – vikingsteve Nov 25 '14 at 14:06
3

enter image description here

If use a

Person extends Student,Prof{}

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

Milaci
  • 515
  • 2
  • 7
  • 24
  • 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 :( – Milaci Nov 25 '14 at 14:21
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.

nunofmendes
  • 3,731
  • 2
  • 32
  • 42
  • 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. – Joeblade Nov 25 '14 at 14:14
  • I've corrected my use of multiple inheritance :) – nunofmendes Nov 25 '14 at 14:17
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
brso05
  • 13,142
  • 2
  • 21
  • 40
1

there is no multiple inheritance in this....

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

user2408578
  • 464
  • 1
  • 4
  • 13
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 
splrs
  • 2,424
  • 2
  • 19
  • 29
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.

univise
  • 509
  • 2
  • 11
0

enter image description here

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

Jamsheer
  • 3,673
  • 3
  • 29
  • 57
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.

Code Geek
  • 485
  • 1
  • 5
  • 15