Inheritance in Java

  • Creating a new class from existing class is known as
  • Inheritance is used to achieve dynamic binding and
  • The class which is inherited is known as super class or parent
    class and the class derived from the parent class is known as sub class or
    child class.
  • Inheritance is implemented using extends keyword.
    Just take a look on below syntax.
class parent_class_name extends child_class_name


  • All the properties of
    parent class come to child class if they are not private.
  • An example about how inheritance is implemented in java is
    given below.
class parent
class child extends parent
static void main(String…s)
c=new child();

  • In above example variable name is default
    therefore it is accessible in child class. If we make name as private
    then it will show error like “name has private access in parent”.

Types of Inheritance in Java

  • There are three types of inheritance in java: single,
    multilevel and hierarchical inheritance.
  • In java, multiple inheritance is not supported by class.
    Multiple inheritance can be implemented by Interface, later on we will discuss
    about it.
Types of Inheritance in Java

Why multiple inheritance is not supported in java?

  • In order to decrease the complexity and ambiguity, multiple
    inheritance is not supported in java.
  • Consider a situation in which we have three classes A, B and
    C. Class C inherit class A and B. If classes A and B have a method with same
    prototype and when we call this method in class C then it will cause ambiguity because
    compiler will be confused that whether method from class A is called or method
    from class B is called.

Leave a Comment

Your email address will not be published. Required fields are marked *