Inheritance. Basic concepts. Superclass and subclass. The keyword ‘extends’. Hiding data in derived classes. Access modifiers private, protected, public
Contents
- 1. What is inheritance?
- 2. What class in Java technology is the superclass for all newly created classes?
- 3. What is the class inheritance syntax? General form of inheritance
- 4. What access modifiers can be used for data members and class methods when inherited?
- 5. What are the features of the use of ‘private‘ access modifier in the case of inherited (derived) classes?
- 6. What are the features of using the ‘protected’ access modifier in the case of inherited classes?
- 7. What are the features of using the ‘public’ access modifier for inherited classes?
- 8. An example that demonstrates access to data of a superclass from a subclass
- 9. What type of access does a data member or class method have that does not have an access modifier in its declaration? Example
- 10. Can a class inherit another class that is implemented in another package?
- Related topics
Search other websites:
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 orsuperclass– it is a class that serves as the basis for another class (classes). In Java terminology, the base class is called the superclass;
- derived class or subclass – this is a class that inherits the superclass code. In Java terminology, the inheriting class is called a subclass. The subclass has the ability to supplement the superclass with additional fields and methods. Also, the subclass has the ability to “overload” or override the methods of the superclass.
Inheritance is organized by the compiler.
2. What class in Java technology is the superclass for all newly created classes?
If a new class is created that does not have a superclass, then this class is always derived from the standard Object class. The Object class is the superclass 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 subclasses 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 subclass. However, there is no access from the object of subclass;
- public. In this case there is an access from the given class, the subclass, the object of the given class and also the object of the subclass.
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 (subclasses), 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 subclass. This is also the case if the subclass is declared in a different package;
- accessible from instances (objects) of this class;
- accessible from instances (objects) of a subclass.
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 subclass;
- objects of this class;
- objects of subclass;
- objects that are declared in class methods that are in other packages.
8. An example that demonstrates access to data of a superclass from a subclass
The example demonstrates access to data members of superclass A from a subclass 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 subclasses. In our case, class B is subclass. Therefore, from the methods of subclass B, you can use the data member b of superclass A;
- c – declared as ‘public’. Access to this data member is possible from class A methods, subclass B methods, class A objects, and subclass B objects.
Below is the code that demonstrates the use of access modifiers private, protected, public
// class A is superclass 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; // allowed, b declared as protected objA.c = 13; // 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 (package access). This also applies to subclasses 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 subclasses.
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
- Reusing code in Java. The concept of composition, inheritance, delegation. The keyword extends. Examples
- Packages. Using packages in Java. Directives import and package. Compiled modules (.java). Intermediate .class files. Project structure. Using standard Java libraries
- Controlling access to classes in packages. Package level access to the class. Public class access level