Exploring the Core of Java: A Comprehensive Guide ( PART - 2)

Java OOPs Concept

Object-Oriented Programming is a paradigm that provides many concepts, such as

  1. class

  2. object

  3. Inheritance

  4. polymorphism

  5. Encapsulation

  6. Abstraction

Class

A class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods common to all objects of one type.

object

Any entity that has a state and behavior is known as an object. For example, a chair, pen, table, keyboard, bike, etc. It can be physical or logical.

Inheritance

It is the mechanism in Java by which one class is allowed to inherit the features (fields and methods) of another class. We are achieving inheritance by using the extends keyword. Inheritance is also known as “is-a” relationship.

Why Do We Need Java Inheritance?

  • Code Reusability: The code written in the Superclass is common to all subclasses. Child classes can directly use the parent class code.

  • Method Overriding: Method Overriding is achievable only through Inheritance. It is one of the ways by which Java achieves Run Time Polymorphism.

  • Abstraction: The concept of abstract where we do not have to provide all details is achieved through inheritance. Abstraction only shows the functionality to the user.

Let us discuss some frequently used important terminologies:

  • Superclass: The class whose features are inherited is known as superclass (also known as base or parent class).

  • Subclass: The class that inherits the other class is known as subclass (also known as derived or extended or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.

Demonstration of Inheritance :

//base class or parent class or super class 
class A{ 
  //parent class methods 
  void method1(){} 
  void method2(){} 
} 
//derived class or child class or base class 
class B extends A{  //Inherits parent class methods 
  //child class methods 
  void method3(){} 
  void method4(){} 
}

Inheritance Types

Below are the different types of inheritance which are supported by Java.

  1. Single Inheritance

  2. Multilevel Inheritance

  3. Hierarchical Inheritance

  4. Multiple Inheritance

  5. Hybrid Inheritance