Why does java doesnot support multiple inheritance?

Answer Posted / deep

Multiple inheritance Oops i think diamond problem..


class A {
public void x() {
System.out.println("Hello");
}

class B extends A {
public void x() {
System.out.println("Hello");
}

class C extends A {
public void x() {
System.out.println("Hello");
}

// Main class
class AB extends B,C // Remember this is not possible in java, a class can extend only one class in java..
{
public static void main(String args[]) {

AB a = new AB();
a.x(); // compiler gets confused whom should i call.. which methods should i invoke since both methods has same name i.e public void x(), complier get scared and raise an error of ambiguity
}
}

To avoid this type of situation, designers of java decided that a class can extends only one class so there would be no ambiguity of methods.. and to over come this issue they added interface..

class can implements n number of interface but can extend only one class.. a interface can extends n number of interface..

Variables declared inside interface are by default,, static and final.. and methods are abstract i.e unimplemented methods, that doesn't have body.. methods of interface are by default abstract and public..

you can make interface public or depends on your requirement.. if you want a interface to access outside of another package better make it public cause non public classes are confined within same package..

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

how we can make a read-only class in java?

791


How do you remove duplicates from an array in java?

745


What are the various access specifiers in java?

794


List the three steps for creating an object for a class?

750


What is the purpose of a volatile variable?

790


What is vector capacity in java?

865


How do I run java on windows?

810


When will you define a method as static in Java?

796


What is :: operator in java?

739


What is not object oriented programming?

718


What are the types of collections in java?

817


What is null data type?

742


Difference between string, string builder, and string buffer?

783


Break statement can be used as labels in java?

808


How many types of operators are there?

756