Unit 3
Unit 3
1. Single inheritance
2. Multilevel inheritance
3. Hierarchical inheritance
4. Multiple inheritance using interfaces
Nested interface
• It is an interface that is defined in the body of a class or interface.
• It is referred to by the outer interface or class and cannot be accessed
directly.
class X{
interface y{}
}
class A implements X.y { }
interface type.
• InterfaceReferences.java
Nested Interfaces
• An interface may be declared as a member of a class or in another
• interface.
• In nested concepts interfaces can only be declared as public or with
default access.
• Syntax of nested interface in another interface is given as
Inheritance of Interfaces
• One interface can inherit another by use of the
keyword extends. The syntax is the same as like
inheriting classes.
• When a class implements an interface that inherits
another interface, it must provide implementations
for all methods defined within the interface
inheritance chain.
Default Methods in Interfaces
• Before Java 8, interfaces could
have only abstract methods.
• The implementation of these
methods has to be provided by
sub class.
56
Use of toString()
• The toString() method returns the String
representation of the object.
• If you print any object, Java compiler internally
invokes the toString() method on the object.
System.out.println(obj);
• So overriding the toString() method, returns
the desired output, it can be the state of an
object etc. depending on your implementation.