Inheritance is one of the main four pillars (core concepts) of OOPs (Object Oriented Programming) concepts in Java. It is a technique of organizing information in a hierarchical form. Java Inheritance provides a reusability mechanism in object-oriented programming for the users to reuse the existing code within the new applications. It does this by allowing the programmer to build a relationship between a new class and an existing class and define a new code in terms of existing code. This relationship is known as parent-child relationship in java. === What is Inheritance in Java OOPs? === The technique of creating a new class by using an existing class functionality is called inheritance in Java. In other words, inheritance is a process by which a child class acquires all the properties and behaviors of the parent class The existing class is called parent class (a more general class). The new class is called child class (a more specialized class). The child class inherits data and behavior from the parent class. === What is Is-A relationship in Java? === Is-A relationship in java represents Inheritance. It is implemented in Java through keywords extends (for class inheritance) and implements (for interface implementation). All the classes extends java.lang.object by default. This is a very good example of Is-A relationship in java. This means an object is the root class of all the classes in Java. Therefore, by default, every class is the subclass of java.lang.object. every class acquires its properties from the object class For example : class A { // statements } The Java compiler will treat the above code like this: class A extends Object{ // statements. } === Important Terminology in Java Inheritance === [1] Superclass/Parent class: The class from where a subclass inherits features is called superclass. It is also called base class or parent class in java. [2] Subclass/Child class: A class that inherits all the members (fields, methods, and nested classes) from other class is called subclass. It is also called a derived class, child class, or extended class. A derived class extends its features by inheriting all the features of another class, called base class, and can also add its own features. The base class remains unchanged. === Creating Subclass in Java or Extending a class === A subclass can be created by using “extends” keyword. Syntax : class subclassName extends superclassName { // Variables of subclass // Methods of subclass } Where, class, and extends are two keywords. The extends keyword indicates that you are deriving a new class (Derived class) from the existing class (Base class). Note: Inheritance is a compile-time mechanism. A parent class can have any number of derived class but a derived can have only one parent class. Therefore, Java does not support multiple inheritance. [3] Re-usability: A mechanism that facilitates to reuse fields and methods of the existing class into the new class is known as reusability. When you create a new class, you can use the same fields and methods already defined in the previous class. This is called reusability. In other words, inheritance allows an object to reuse the code of another object and also allows to add its own features as well. === Why do we need/use Inheritance in Java? === [1] We can reuse the code from the base class. [2] Using inheritance, we can increase features of class or method by overriding. [3] Inheritance is used to use the existing features of class. [4] It is used to achieve runtime polymorphism like method overriding. [5] Using inheritance, we can organize the information in a hierarchal form. === How is Inheritance implemented/achieved in Java? === Inheritance in Java can be implemented or achieved by using two keywords: 1) extends: extends is a keyword that is used for developing the inheritance between two classes and two interfaces. Note that a class always extends another class. An interface always extends another interface and can extend more than one interface. 2) implements: implements keyword is used for developing the inheritance between a class and interface. A class always implements the interface. === Advantage of Inheritance in Java === [1] One of the main advantages is that you can minimize the length of duplicate code in an application by putting the common code in the superclass and sharing amongst several subclasses. [2] Due to reducing the length of code, redundancy of the application is also reduced. [3] Inheritance can also make application code more flexible to change because a class that inherits from the superclass, can be used interchangeably. #JavaOOPsConceptsTutorials #LearningandTeachingCoding