1. Basic syntax for inheritance

1.1. What is inheritance

Inheritance is a mechanism in object-oriented languages for creating new classes from existing classes, namely:

A new class can get its own from an existing classattributeandmethods.

  • This new class is called a subclass, or derived class;
  • The existing class is called the parent class, also called the base class;

Here’s an example:

1.2. Why inheritance

  • Code gets great reuse;
  • Form a hierarchy of classes;
  • Better simulates the real world (genetic);

1.3. Basic syntax for inheritance

Inheritance of classes in Java uses the extends keyword, which has the following syntax:

//subclass indicates a subclass
// Superclass indicates the parent class from which a subclass inherits

class <subclass> extends <superclass>
{}Copy the code

1.4. Is multiple inheritance allowed in Java?

Java allows only single inheritance, not multiple inheritance.

  • Single inheritance: a subclass can have only one parent;
  • Multiple inheritance: a subclass with more than one parent class;

1.5. Principles of succession

Inheritance in object – oriented thinking reflects the relationship from general to special in the real world.

For example, in the figure in section 1.1. :

  • Students are a special kind of people, workers are a special kind of people, teachers are a special kind of people;
  • Primary school students are a special kind of students;
  • A professor is a special kind of teacher;

2. Inherited permissions

In the same package, when a subclass inherits its parent class, not all members of the parent class are inherited by the subclass. Instead, it selectively inherits the parent members based on their access control characters:

  • A private member of a parent class cannot be inherited by a subclass. All other members can be inherited.

The test procedure is as follows:

/ * * *@briefTests the same package inherited member access permissions *@author  mculover666
 * @date2019/4/27 * /
class A
{
    int i;
    private int j;
    protected int k;
    public int h;

    void fa(a)
    {}private void fb(a)
    {}protected void fc(a)
    {}public void fd(a)
    {}}class B extends A
{}class TestExtends
{
    public static void main(String[] args) 
    {
        B bb = new B();

        bb.i = 1;
        //bb.j = 2; // Error, private member attributes of the parent class are not inherited
        bb.k = 3;
        bb.h = 4;

        bb.fa();
        //bb.fb(); // Error: member methods private to the parent class are not inheritedbb.fc(); bb.fd(); }}Copy the code

If you comment two lines of code in the main methodbb.jandbb.fb()If this is disabled, an error will be reported at compile time because of access permissions:

3. Call the superclass constructor with super

The constructor of the parent class is not inherited by the subclass, but the subclass can call the constructor of the parent class with the super() keyword to initialize the member attributes. Note two points:

  • The call to super must be the first statement in the constructor;
  • The subclass constructor calls the super() function by default.

Test procedure:

/ * * *@briefTest constructor inheritance *@author  mculover666
 * @date2019/4/28 * /
class A
{
    public int i;
    public int j;
    
    public A(int i,int j)
    {
        this.i = i;
        this.j = j; }}class B extends A
{
    public int k;

    public B(int i,int j,int k)
    {
        /* Important: Subclasses can call the parent constructor */ via super()
        super(i, j);
        this.k = k;
    }
    public void show(a)
    {
        System.out.println("i = "+i+",j = "+j+",k = "+k); }}class TestSuper
{
    public static void main(String[] args) 
    {
        B b1 = new B(1.2.3); b1.show(); }}Copy the code

The running results are as follows:



To receive more exciting articles and resources, please subscribe to my wechat official account: “McUlover666”.