2. INHERITANCE
• INHERITANCE – Inheritance is inheriting the
properties of one class(Parent class) in the another
class(child class). This relation can be defined by
using the extends keyword as −
public class childClass extends Parentclass{
}
• Java is an Object Oriented Programming
language and supports the feature of inheritance.
We cannot have Multiple Inheritance in Java
directly ,but it can be implemented using Interfaces.
3. • The class which inherits the properties is known as sub class or child class and the class whose
properties are inherited is super class or parent class. In inheritance a copy of parent class members
is created in the child class object. Therefore, using the child class object you can access the members
of the both classes . The various types of inheritance in java are :-
• Single
• Multilevel
• hierarchical
• Multiple
• Hybrid
4. Multiple inheritance is inheriting properties of two or more parent classes to one
child class . As given in the below diagram class A and class B is being inherited by
the child class C
CLASS A
CLASS B
CLASS C
5. Sample code of how multiple inheritance works in java is given below. First two classes are
made ParentA and ParentB and they both have same signature methods walk() . a child class is made which is
inheriting both the classes by using a keyword 'extends'
6. OUTPUT :
• In the above code the compiler will get confuse which method to call when object of child class calls
walk() method and results in compile time error.
• The solution to this problem of Multiple inheritance is using Interface
7. INTERFACE IN JAVA
Interface is a collection of abstract methods(non-defined methods). A class implements an interface, hence
inheriting the abstract methods of the interface. An interface may also contain constants, Variables,
default methods, static blocks, methods etc . Interfaces are public ,static and final.
The syntax of interface is given below :-
8. To inherit a class in another class we use the keyword extends,
And to inherit or to implement a interface in class we use implements
keyword.
CLASS
CLASS
INTERFACE
CLASS
EXTENDS IMPLEMENTS
9. • BELOW IS AN EXAMPLE OF WHERE MULTIPLE INHERITANCE IS ACHIEVED WITH THE HELP OF
INTERFACES.
10.
11. OUTPUT :-
• hence, in the above code we have achieved Multiple inheritance in java with the help of interfaces where we have declared
our methods in the interfaces and overridden them in the child class. (override means when a method that has already been
declared in parent class is being defined again in the child class having same name, number and type of parameters, and
return type as the method that it overrides) so, we have re-implemented the walk() and run() method of parent class in the
child class to use them. When the object of child class will call method they will be called without getting ambiguity and will
print the output.
12. Conclusion :-
• Java does not support "multiple inheritance" (a class can only inherit from
one parent class). However, it can be achieved with help of interfaces,
because the class can implement multiple interfaces.