Inheritance in Java | learn inheritance

By | November 14, 2018

Inheritance in Java

Inheritance in Java

Inheritance In Java


Inheritance means inheriting something from someone. In real life, an example is that
a child is inheriting money from his parents. So if a parent has million dollars a child is basically inheriting million dollars from his parents.

Inheritance in Java means a class is inheriting the methods and properties of one class

from the other class. It means a class can access methods and properties in other class from which it extends.
That class is also called parent class or superclass.

In order to understand better let’s create a new class and name it Parent
and create other class and name it child then you should extend child class to
parent class as:

public class Child extends Parent{


child extends Parent means?

Child extends Parent means that child class is inheriting the properties from a parent class.

Above syntax means that all the properties of the parent class can be accessed in a child class.

Extends Keyword:

The extends keyword basically means that allows one class in java to inherit properties
from the other class.

super keyword:

Super keyword is used to access the methods of a superclass.

E.g. If there is a superclass which has a method called move() and we are currently
working in child class so we want to access that method in child class then what
we will do?. Then we will use the super keyword with that function to access.
super keyword is also used to access all those instance variable in child class
from parent class with super keyword. e.g super(firstName,LastName);

Classes and Objects in Java with examples

For example:


Note: You should write those variables in super() which are not available in
the child class and you want to access those parameters in a child class.

By default super or parent class is extended from object class which is invisible
to us.

So, For example, we are creating a class namely Parent Class inside that class we creating function. So let suppose Parent is our superclass and we are now
creating another class name child and then we extend child class from the parent class
. Means we want to use that function which is present in a parent class.

So now we are creating one main class in order to create an object of the child class.

for creating main class go to user projects and then under source packages write
click on your package name and then new and then click on create a main class.

when you create the main class then type your child class name and type any object name
then equal sign and then again write your class name.

so below your child object name type the object name of child class and type the
function or method name which you have created in a superclass.

So print that program you will see that program will run that function which is
present in a superclass and we don’t create in child class so it means we have

inherited that method to child class from superclass.

For example:

//parent class

public class Parent(){

public static void money(){

System.out.println(“money recieved”);


//child class

public class Child extends Parent{


//Java Main Class

public class MainClass{

public static void main(String[] arg){

child tom = new Child();


See Also:

Classes and objects in Java

Leave a Reply

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