Definition

  • Inheritance is the ability of one class or object to possess characteristics and functionality of another class or object in the hierarchy.
  • It is the mechanism in java by which one class is allow to inherit their features(fields and methods) into another class.

Features

  • Java Inheritance is one of the core concepts of Object Oriented Programming.
  • Java Inheritance is used when we have is-a relationship between objects.
  • Inheritance in Java is implemented using extends keyword i.e. To inherit a class into another class extends keyword is used.
  • 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.

    Syntax

    – – –
    – – –
    class Childclassname extends Superclassname  
    {  
         //required methods and fields  
    – – –
    – – – 

    Access Controls/Modifiers in Java

    • There are two types of access controls/modifiers in Java : – access modifiers and non-access modifiers.
    • There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient, etc.
    • Access control is a mechanism, an attribute of encapsulation, which 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 class by applying these access modifier on it.
    • Access modifiers are used to restrict the access of members of a class i.e. data member and methods mainly.
    • There are four access modifiers in Java. They are:
      1. public
      2. protected
      3. default
      4. private
    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.
    • Among the four access modifiers, private is the most restrictive access modifier and public is the least restrictive access modifier. 
    • public – Classes, methods or data members which are declared as public are accessible from every where in the program. There is no restriction on the scope of a public data members. Any class, in any package, can access the public members of a class’s. Declare public members only if we want to provide access to a member by every class.
    • protected – The methods or data members declared as protected are accessible within same package or sub classes in different package i.e. Protected specifiers allows the class itself, sub classes, and all classes in the same package to access the members.A member can be declared protected  using keyword protected.
    • default – In Java, when no access modifier is specified for a class, method or data member, It is said to be having 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 same package will not be able to access these members. To declare a private member, use the private keyword in its declaration.
    • Syntax for declaring a access modifier are : – 
    AccessModifierName  DataType  VariableName;

    Types of Inheritance 

    There are 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 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:

    • Derivation of a class involving 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 behaviour of the parent class. However, sometimes, it is required to modify the behaviour of parent
        class. To modify the behaviour of the parent class overriding concept is used.
      • Some important points that must be taken care 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 subclass.
               iii. Overriding methods must have exactly the same argument lists, both in              type and in order.
               iv. An overriding method must have exactly the same return type as the                   method it overrides.
                v. Overriding is associated with inheritance.

      Use of Inheritance

      • Inheritance is used in Java for method overriding purpose so that runtime polymorphism can be achieved.
      • Inheritance is used for extending the existing property of a class into another class.
      • Inheritance is used for code reusability of pre-existing codes.

       421 total views,  2 views today

      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.