Introduction of Inheritance in Java

  • Inheritance in Java is a fundamental concept in object-oriented programming that facilitates the creation of a hierarchy of classes, promoting code reuse and enabling the creation of more specialized classes based on existing ones.

Definition of Inheritance in Java

  • Inheritance is the ability of one class or object to possess the characteristics and functionality of another class or object in the hierarchy.
  • Inheritance is the mechanism in Java by which one class is allowed to inherit its properties(fields and methods) and behavior into another class.

Features of Inheritance in Java

  • Java Inheritance is one of the core concepts of Object Oriented Programming.
  • Superclass (Base class or Parent class): The class whose properties and methods are inherited is called Superclass.
  • Subclass (Derived class or Child class): The class that inherits from the superclass.
  • Java Inheritance is used when we have is-a relationship between objects.
  • Inheritance can be supported as “run-time” or “compile-time” or both.
  • Inheritance is a language property specific to the object-oriented paradigm.
  • Inheritance is “from generalization- to-specialization” format.
  • extends keyword: Inheritance in Java is implemented using extends keyword i.e. To inherit a class into another class extends keyword is used.

      Syntax of Inheritance in Java

      class Superclassname
      {
           // Properties and methods
      }
        – – –
        – – –
      class Childclassname extends Superclassname  
      {  
           //required methods and fields  
      – – –
      – – – 
      • super keyword :
        • In Java, the super keyword is used to refer to the immediate parent class or invoke –
          • Accessing methods or variables of the superclass from the subclass.
          • Invoking the superclass’s constructor from the subclass’s constructor.
          • Used to initialize superclass-specific properties.
          • Calling a method of the superclass that has been overridden in the subclass.
          • Allows execution of the overridden method from the superclass.
        • super() keyword must be the first statement in the subclass constructor when used to invoke the superclass constructor.
      • this keyword :
        • In Java, the ‘this’ keyword is also called a reference variable.
        • this keyword refers to the current object in a class(such as the current class instance variable, current class method, current class constructor, to return the current class instance, passing as an argument for a method/constructor, etc.).

      Access Specifiers/Controls/Modifiers in Java

      • There are two types of access controls/modifiers in Java:- Access Modifiers (such as private, protected, default/package, and public) and Non-Access Modifiers (such as static, abstract, synchronized, native, volatile, transient, etc.)
      • Access control is a mechanism, an attribute of encapsulation, that restricts the access of certain members of a class to specific parts of a program.
      • Access to members of a class can be controlled using the access modifiers.
      • We can apply access modifiers to classes, variables/fields/data members, methods, constructors, etc.
      • We can change the access level of fields, constructors, methods, and classes by applying these access modifiers to it.
      • Access modifiers are used to restrict the access of members of a class i.e. data members and methods mainly.
      • There are four access modifiers in Java. They are:
        • public
        • protected
        • default
        • private
        • The syntax for declaring an access modifier are: –
      AccessModifierName  DataType  VariableName;
      AccessModifierName  ReturnType  MethodName;
        • Among the four access modifiers, private is the most restrictive access modifier and public is the least restrictive access modifier. 
        • public :
          • They are mostly declared with classes, methods, variables, or data members of the classes.
          • They are accessible from everywhere in the program.
          • There is no restriction on the scope of public data members.
          • Any class, in any package, can access the public members of a class.
          • This access specifier is declared or used only when we want to provide access to a member by every class.
          • A member can be declared public using the keyword ‘public’.
        • protected :
          • The methods or data members declared as protected are accessible within the same package or sub-classes in different packages.
          • Protected specifiers allow the class itself, sub-classes, and all classes in the same package to access the members.
          • A member can be declared protected using the keyword ‘protected’.
          • The protected modifier is more restrictive than the public. 
        • default :
          • In Java, when no access modifier is specified/mentioned for a class, method, or data member, It is said to have the default access modifier by default.
          • The members with default (also known as Package Private or no modifier) access modifier are accessible only within the same package.
          • The default modifier is more restrictive than protected. 
        • private :
          • The methods or data members declared as private are accessible only within the class in which they are declared.
          • Any other class of the same package will also not be able to access these members.
          • A member can be declared private using the keyword ‘private’.
          • The private modifier is more restrictive than the default, i.e. the highest restriction. 
      Access Modifier/ Specifier Within the Same Class Other Class within the Package In Sub Classes(Within the Package and Other Packages) Any Class/World/In other packages
      Public Y Y Y Y
      Protected Y Y Y N
      Package/Default Y Y

      Same Package= Y

      Other Package= N

      N
      Private Y N N N
      • NB:  Y = Accessible, N = Not Accessible.

      Types of Inheritance 

      There are the following types of inheritance shown in Java –

      (i) Single Inheritance:

      •  When a class is derived/inherits the features from only one base class, it is called Single       Inheritance.
      • A derived class can be declared only if its base class is already declared.

      (ii) Multiple Inheritance:

      • Java does not support this type of Inheritance but this feature is done with the help of an interface.

      (iii) Multi-level Inheritance:

      • When a class is derived/inherits the features from another derived class, is called Multi-level Inheritance.

      (iv) Hierarchical Inheritance:

      • Derivation of several new child classes from a single base class is called hierarchical inheritance.

      (v) Hybrid Inheritance:

      • The derivation of a class involving at least more than one form of inheritance is called hybrid inheritance.

        Method overriding

        • In Inheritance, Most of the time the purpose of inheriting properties from the parent class and adding new methods is to extend the behavior of the parent class. However, sometimes, it is required to modify the behavior of the parent class. To modify the behaviour of the parent class overriding concept is used.
        • Some important points that must be taken care of by us while overriding a method: –
                 i. An overriding method (largely) replaces the method it overrides.
                 ii. Each method in a parent class can be overridden at most once in any one of the subclasses.
                 iii. Overriding methods must have the same argument lists, both in type and in order.
                 iv. An overriding method must have the same return type as the method it overrides.
                  v. Overriding is associated with inheritance.

        Advantage/Use of Inheritance

        • Code Reusability: Inherited methods and properties can be reused (according to need) without rewriting again in subclasses.
        • Extensibility: Inheritance allows the addition of new methods or fields to a subclass without modifying the superclass.
        • Polymorphism: Inheritance enables a subclass to override methods of the superclass to provide specific implementations. In other words, Inheritance helps for method overriding purposes so that runtime polymorphism can be achieved.

          Loading

          Categories: Java

          0 Comments

          Leave a Reply

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

          This site uses Akismet to reduce spam. Learn how your comment data is processed.