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?
Options:
- Multithreading
- Interfaces
- Protected methods
- Private 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
Using which of the following, multiple inheritance in Java can be implemented?OptionsMultithreadingInterfacesProtected methodsPrivate methods
Interfaces in Java can be used to achieve:Question 4Answera.Multiple inheritance.b.Code encapsulation.c.Method overloading.d.Static method implementation.
answerWhich of the following is not a benefit of inheritance in Java?OptionsReusability of codeEncapsulationPolymorphismCode organization
Which of these is supported by method overriding in Java?Select one:AbstractionEncapsulationPolymorphismNone of the mentioned
rrect answerWhich of the following is used for implementing inheritance through class?Optionsinheritedextendsusingimplements
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.