Inherited a constructor

8.2 Inheritance in Java

The inheritance relationship is in Java with the keyword extends described. Example:

class employees extends Person {/ * class implementation * /}

The class Employee is a specialization of the class person. It inherits the public attributes and methods of the class person.

All classes in Java inherit directly or indirectly from the Java base class Object. Will not be for a class declaration extends Clause specified so the class is automatically assigned by the class Object derived.

... provides a minimal infrastructure for all Java classes:

  • the possibility of dynamically creating objects on the heap
  • an identity and the associated possibility of comparing objects
  • the possibility of automatic deletion if there is no longer a reference to an instance.
  • Possibility of instrospection (dynamic exploration of the structure of the class)

Upper classes inherit public (public, protected) Methods and attributes on subclasses. This means that they can be used like your own attributes.

The example class person has:

  • private attributes for first and last names
  • public method for reading the entire name
  • public method for setting the name
  • a public attribute for managing age
  • A constructor to set the name

The upper class person

package s1.block8; public class Person {private string name; private string firstName; public int age; public person (string ln, string fn) {name = ln; firstName = fn; } public Person () {this ("Doe", "John");} public void setName (String ln, String fn) {name = ln; firstName = fn; } public String fullName () {return (name + "" + firstName);}}

The class Employee should now have the public properties of the upper class person use. In this way, redundant programming of the existing functionality can be avoided.

The subclass Employee

package s1.block8; public class Employee extends Person {private String employeeId; public Employee (String ln, String fn, String EmpId, int a) {super (ln, fn); // Java calls the constructor of the superclass here employeeId = EmpId; age = a; } public String getEmployeeId () {return employeeId;} public String printAll () {return (fullName () + "" + employeeId + "" + age); } public static void main (String [] args) {
Employee ceo = new Employee ("Doe", "Jane", "1", 25);
Employee cto = new Employee ("Miller", "John", "2", 30);

cto.age ++;
System.out.println (ceo.printAll ());
System.out.println (cto.printAll ());
} }

Use of the class

The class implemented in this way can be used as follows:

Doe Jane 1 25 Miller John 2 31

All public attributes and methods (example age) the class person can in class Employee be used as if they are your own / local.

However, inheritance does not work backwards. You can have an instance of person no employee number (employeeId) to assign. It exists in the class person Not.

"Contract Law": All instances of Employee must also look exactly like instances of person behavior. They are also instances of the class person. Other consumers in the program rely on this behavior, this "contract". This means:

  • The subclass may have to pay attention to forced initializations of the superclass by recursively calling its constructors.
  • Published attributes and methods can / should no longer be blocked. Example: In a company, the age is not allowed to be published due to legal regulations

Inheritance in UML

Implementation without inheritanceImplementation with inheritance

Here is an implementation of the class Employee without inheritance. The implementation corresponds to the right implementation with inheritance. However, the methods of the class have direct access to the private attributes Surname, first name.

An implementation of the class Employee with inheritance of the class person Inheritance

Inheritance of private attributes and methods

Private attributes are not inherited!

They are only visible within the class. A subclass does not have privileged access to private methods. They are not inherited. Like any other class, a subclass must access the private attributes using existing public access methods.

Constructors are not inherited in Java. You can only call them indirectly via the new () operator for a given class.

Constructors must be implemented for each subclass. However, the constructors of the subclass must be able to call existing constructors of the superclass, since they are necessary for the initialization of the superclass. To do this, the keyword Super() used.

With this you can now take the class Employee () more elegant and robust initialization

public class Employee extends Person {private String employeeId; public Employee (String ln, String fn, String EmpId, int a) { super (fn, ln); // Java calls the constructor of the superclass here employeeId = EmpId; age = a; } public void printAll () {System.out.println (fullName () + "" + employeeId + "" + age); }

Important: The call to the constructor of the superclass must be the first command in the constructor of the subclass. The attributes of the superclass must be initialized first before the attributes of the subclass are initialized. This is the only way to ensure that instances of the upper class behave correctly. If the call to the constructor of the superclass were not the first, the subclass could incorrectly manipulate the superclass.

Subclass constructors without Super() call

Constructors do not have to explicitly call a specific constructor of the superclass.

Will not Super() If the call is implemented, the translator automatically generates one Super() Call that calls the standard (default) constructor.

This ensures that the class hierarchy is always run through completely during initialization.

If the superclass only has constructors with transfer parameters, one of these constructors must have Super() be called.

In the following example you can see the initialization chain of a class C which is derived from B. B is derived from A:

public class A {public A () {System.out.println ("Constructor A called");}} public class B extends A {public B () {System.out.println ("Constructor B called");}} public class C extends B {public C () {System.out.println ("Constructor C called");}}

Creating an instance of C:

C c = new C ();

Triggers the following outputs:

Constructor A called Constructor B called Constructor C called