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());
}
}