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

What is java command?

574


What is a type parameter in java?

538


What is stack example?

549


What is a module function?

562


What are the two types of java?

525






What is difference between printf and scanf?

523


What is advantage of java?

547


What is the purpose of using break in each case of switch statement?

571


Can we sort hashset in java?

633


How to sort array of 0 and 1 in java?

547


Can you declare a private method as static?

703


What is treeset in java?

550


Given a singly linked list, find the middle of the list in a single traversal without using temporary variable.

619


What is an interoperable application in java ?

590


What is the difference between static (class) method and instance method?

580