0% found this document useful (0 votes)
2 views3 pages

Java Assignment 10

The document explains multiple inheritance in Java, highlighting that Java does not support it for classes but allows it through interfaces. It discusses the concept of Java interfaces, their syntax, and the reasons for Java's restriction on multiple inheritance, including the diamond problem, complexity, and ambiguity. An example program demonstrates how to implement multiple interfaces in Java, showcasing the use of the super keyword.

Uploaded by

sy2649721
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Java Assignment 10

The document explains multiple inheritance in Java, highlighting that Java does not support it for classes but allows it through interfaces. It discusses the concept of Java interfaces, their syntax, and the reasons for Java's restriction on multiple inheritance, including the diamond problem, complexity, and ambiguity. An example program demonstrates how to implement multiple interfaces in Java, showcasing the use of the super keyword.

Uploaded by

sy2649721
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Assignment_9: - Program to demonstrate multiple inheritance using interfaces (Use

super keyword).

What is Multiple Inheritance?

Multiple inheritance is the process where our subclass inherits more than one superclass.
Unlike some other programming languages, like C++, Java does not support multiple
inheritance of classes. This means Java cannot inherit multiple superclasses. Java has
interfaces.

Java interfaces
Java interfaces are used to achieve abstraction, which is one of the principles in
Object-Oriented Programming. The Java interface method does not have a Java body. It is
also used to implement Multiple Interfaces in the Java language. In other words, interfaces
are the methods and variables. They cannot have a method definition or body.

Syntax of Java Interfaces.

interface

// declare constant fields

// declare methods that abstract

// by default.
}

Syntax of implementing multiple interfaces.


class MyClass implements Interface1, Interface2, Interface3

// class body

Why is Multiple Inheritance Not Supported in Java?


In Java, Multiple Inheritance is not supported by default due to several ambiguity issues.
Why this happened with Java.

 Diamond Problem: One of the well-known issues in Multiple Inheritance is the


diamond problem. This problem occurs when a class inherits from two classes that
have a common method. If both parent classes have the same method with the same
name and the same parameters. Then the java interpreter can’t determine which
method to call in the child class.

 Complexity: Multiple Inheritance creates more complex problems and hierarchies,


making it harder to understand and maintain code. Java focuses on keeping the
language simple and easy to learn, so it does not include features like multiple
inheritances that can increase complexity.

 Ambiguity: Allowing multiple inheritance can lead to ambiguity in method


resolution. If a subclass inherits methods from two superclasses with the same
parameter but different implementations, it is unclear if implementation occurs. This
ambiguity can cause errors and make code hard to reason about.

interface A

public void addition (int num1);

interface B

public void addition (int num1);

}
class C implements A, B

public void addition (int num1)

[Link]("Output = "+num1);

public class MultpleInheritance

public static void main (String [] args)

C obj = new C ();

[Link](125);

Output

Copy

Output = 125

You might also like