with Verified Solutions
What is an inner class?
✔✔ An inner class is a class defined within another class.
Can an inner class access private members of its outer class?
✔✔ Yes, an inner class can access all members of its outer class, including private ones.
What is a static nested class?
✔✔ A static nested class is a class declared inside another class but does not have access to
instance members of the outer class.
Can a static nested class access instance variables of the outer class?
✔✔ No, a static nested class can only access static members of the outer class.
What is an anonymous inner class?
✔✔ An anonymous inner class is a class without a name that is defined and instantiated in a
single statement.
1
,When would you use an anonymous inner class?
✔✔ When you need a short-lived implementation of an interface or subclass.
What is the benefit of using interfaces?
✔✔ Interfaces promote loose coupling and allow multiple inheritance of behaviors.
What is the relationship between an interface and a class that implements it?
✔✔ The class provides implementations for the methods defined in the interface.
Can an interface extend a class?
✔✔ No, an interface cannot extend a class, only other interfaces.
Can an interface contain static methods?
✔✔ Yes, interfaces can have static methods with implementations.
Can you create an instance of an interface?
2
,✔✔ No, you cannot instantiate an interface directly, but you can create an object of a class that
implements it.
What is a marker interface?
✔✔ A marker interface is an interface with no methods, used to provide metadata to classes.
What is inheritance in Java?
✔✔ Inheritance is a mechanism that allows one class to acquire the properties and behaviors of
another class.
What keyword is used to create a subclass in Java?
✔✔ The `extends` keyword is used to create a subclass.
What is the purpose of the `super` keyword?
✔✔ The `super` keyword is used to call the constructor or methods of the parent class.
Can a subclass override a private method of its superclass?
✔✔ No, private methods are not inherited and cannot be overridden.
3
, What is method overriding?
✔✔ Method overriding is when a subclass provides a new implementation for a method inherited
from its superclass.
What is the difference between method overloading and method overriding?
✔✔ Method overloading occurs in the same class with different parameter lists, while method
overriding occurs in a subclass with the same method signature as in the superclass.
Can a subclass have more methods than its superclass?
✔✔ Yes, a subclass can define additional methods that are not present in the superclass.
What happens if a subclass does not define a constructor?
✔✔ The default constructor of the superclass is called automatically.
What is the purpose of the `final` keyword in inheritance?
✔✔ The `final` keyword prevents a class from being subclassed and a method from being
overridden.
4