Using which of the following, multiple inheritance in Java can be implemented?OptionsMultithreadingInterfacesProtected methodsPrivate methods
Question
Using which of the following, multiple inheritance in Java can be implemented?OptionsMultithreadingInterfacesProtected methodsPrivate methods
Solution
Interfaces can be used to implement multiple inheritance in Java. Here's a step-by-step guide on how to do it:
Step 1: Define the interfaces. In Java, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types.
interface Interface1 {
void method1();
}
interface Interface2 {
void method2();
}
Step 2: Create a class that implements both interfaces. In Java, a class can implement multiple interfaces by using the implements keyword.
class MyClass implements Interface1, Interface2 {
public void method1() {
System.out.println("Method 1");
}
public void method2() {
System.out.println("Method 2");
}
}
Step 3: Create an object of the class and call the methods.
public class Main {
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.method1();
myClass.method2();
}
}
When you run the Main class, it will output:
Method 1
Method 2
This shows that the MyClass object is able to access both method1 from Interface1 and method2 from Interface2, demonstrating multiple inheritance in Java through interfaces.
Similar Questions
Select the correct answerUsing which of the following, multiple inheritance in Java can be implemented?OptionsInterfacesMultithreadingProtected methodsPrivate methods
Which keyword is used to implement inheritance in Java?Optionsextendsinheritimplementsinheritance
Does Java support multiple level inheritance?OptionsFalseTrue
e correct answerUsing which of the following, multiple inheritance in Java can be implemented?
In which scenario does Java NOT allow polymorphism?OptionsWhen using the method overloadingWhen overriding private methodsWhen using interface methodsWhen using abstract methods
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.