Inheritance. Basic concepts. Base class and derived class. The keyword ‘extends’. Hiding data in derived classes. Access modifiers private, protected, public

Inheritance. Basic concepts. Base class and derived class. The keyword ‘extends’. Hiding data in derived classes. Access modifiers private, protected, public


Contents


 1. What is inheritance?

In terms of object-oriented programming, inheritance is the use of a software class of another class (s) for the purpose of its use, modification, extension. Inheritance is one of the advantages of object-oriented programming in comparison with procedural programming. When inheriting, a new derived class is a specialization of an already existing class.

In the case of inheritance, there are generally two classes:

  • base class – it is a class that serves as the basis for another class (classes);
  • derived class – this is a class that inherits the base class code. The derived class has the ability to supplement the base class with additional fields and methods. Also, the derived class has the ability to “overload” or override the methods of the base class.

Inheritance is organized by the compiler.

 

2. What class in Java technology is the base for all newly created classes?

If a new class is created that does not have a base class, then this class is always derived from the standard Object class. The Object class is the base class for all newly created Java classes.

 

3. What is the class inheritance syntax? General form of inheritance

When class is inherited, the ‘extends’ keyword is used. If class B inherits the data and methods of class A, the inheritance syntax is as follows:

class A
{
    // ...
}

class B extends A
{
    // ...
}

 

4. What access modifiers can be used for data members and class methods when inherited?

The following access modifiers can be applied to data members and class methods:

  • private. In this case, the data member of a class or a class method is available only from the methods of this class. There are no access in methods of the derived classes and objects (instances) of this class;
  • protected. In this case there is an access from the given class, from object of this class and also from the inherited class. However, there is no access from the object of derived class;
  • public. In this case there is an access from the given class, the derived class, the object of the given class and also the object of the derived class.

Also, a data member of a class or a class method can be declared in a class without using any modifier (see clause 9).

 

5. What are the features of the use of ‘private‘ access modifier in the case of inherited (derived) classes?

 If data members or class methods are declared with the private access modifier, then they are considered:

  • accessible from the methods of the class in which they are declared;
  • inaccessible to all other methods of any classes (inherited), instances (objects) of any classes.

 

6. What are the features of using the ‘protected’ access modifier in the case of inherited classes?

If data members or class methods are declared with the ‘protected’ access modifier, then they are considered:

  • accessible from the methods of the class in which they are declared;
  • accessible from the methods of the inherited class. This is also the case if the inherited class is declared in a different package;
  • accessible from instances (objects) of this class;
  • inaccessible from instances (objects) of a class that was inherited from this.

 

7. What are the features of using the ‘public’ access modifier for inherited classes?

The public access modifier is used if you need to access a data member or a class method from anywhere in the program. Access to public data members and class methods have:

  • methods of this class;
  • methods of inherited class;
  • objects of this class;
  • objects of inherited class;
  • objects that are declared in class methods that are in other packages.

 

8. An example that demonstrates access to data of a base class from an inherited class

The example demonstrates access to data members of class A from a derived class B. Class B inherits data members of class A.

Class A contains three integer data members that are declared with different access modifiers:

  • a – declared as ‘private’. Access to this data member is possible only from class A methods;
  • b – declared as ‘protected’. Access to such a data member is possible only from class A methods and derived classes. In our case, class B is derived. Therefore, from the methods of class B, you can use the data member b of class A;
  • c – declared as ‘public’. Access to this data member is possible from class A methods, derived class B methods, class A objects, and derived class B objects.

Below is the code that demonstrates the use of access modifiers private, protected, public

// class A is base class for class B
class A
{
    private int a;
    protected int b;
    public int c;
}

// class B inherits class A
public class B extends A
{
    void AccessDemo()
    {
        // a = 5; // error, a - hidden member of class data (private)
        b = 10; // allowed, b declared as protected data member (protected)
        c = 15; // allowed, c - public data member
    }

    public static void main(String[] args)
    {
        // demonstration of access to fields of class A
        B objB = new B(); // object of class B
        A objA = new A(); // object of class A

        // 1. Access to fields from a class A object
        // objA.a = 15; // not allowed, error, data member is declared as private
        objA.b = 17; // not allowed, b declared as protected
        objA.c = 13; // not allowed, c declared as public

        // 2. Access to fields and methods from the object of class B
        objB.AccessDemo(); // call the method of class B
        // objB.a = 30; - error, a - private data member of class
        // objB.b = 30; - error, b - protected data of class
        objB.c = 25; // allowed, because c in class A is declared public

        int d = objB.c; // d = 25
        int d2 = objA.b; // d2 = 17
    }
}

 

9. What type of access does a data member or class method have that does not have an access modifier in its declaration? Example

Before declaring a member of a data class, it is not necessary to set an access modifier. There is a case when the access modifier is not assigned.

In this case, the data member of the class or the class method is:

  • accessible from the methods of this class;
  • accessible from all class methods that are implemented in this package (packet access). This also applies to derived classes that are implemented in this package. In this case, the access type is considered as public, but within the package. Outside the package, such an element of the class is not accessible;
  • not accessible to any methods of other classes that are implemented in other packages. This also applies to derived classes.

Example. The declaration of data member of class C without an access modifier is demonstrated.

public class C
{
    double x; // data member declaration x without access modifier
    // ...
}

In this case, the data member x of class C is visible within the package. More details about packages in Java are described here.

 

10. Can a class inherit another class that is implemented in another package?

Yes. In this case, the inheritance rules are the same as in the case of classes implemented in the same package.


Related topics