INHERITANCE

An object of one class behaving as an Object of Another Class.

The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.

Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

Terms used in Inheritance

  • Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
  • Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
  • Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
  • Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.

Access Modifiers:

publicThe code is accessible for all classes
privateThe code is only accessible within the declared class
defaultThe code is only accessible in the same package.
protectedThe code is accessible in the same package and subclasses.

What is extends in Java inheritance?

The extends keyword extends a class (indicates that a class is inherited from another class).

In Java, it is possible to inherit attributes and methods from one class to another.

  • subclass (child) – the class that inherits from another class
  • superclass (parent) – the class being inherited from

Leave a comment

Design a site like this with WordPress.com
Get started