Detailed inheritance (under) - super keyword and polymorphism

Connect Bowen - "Detailed inheritance (on) - abstract and layered tool"
ado, into the topic:

In Part I "trick", with a super (); solve the problem, this is why?
The answer: all of the subclass constructor defaults will access the parent class constructor empty parameter
(expansion: Because of this principle, we do in the future " tools " must be to bring the no-argument constructor)

Well, not the parent class constructor with no arguments, a subclass of how to do?

No means solve the parent's constructor with no arguments :

  • In the parent to add one constructor with no arguments
  • Subclass to call the parent class constructor with other parameters of the super
  • Subclass through this to call this class of other constructors (this other class structure must first access the parent class constructor)

Well, now, I have to explain it super:

super:

super class is actually the meaning of the superclass, base class, parent class.

I am here to remind you that:

Object class is the base class for all classes

There are two super strict requirements:

  1. It can only appear in the constructor;
  2. If super (), it must be the first statement constructor
    (so, super (), and this () can not occur simultaneously in the same constructor)

And, whether we constructor in the subclass is no parameters or with parameter, by default, JVM calls the only non-parametric base class constructor!

We mentioned at the beginning of the blog articles before, we can "inherit merit" in the "inheritance" of the process, then I took off now in terms of how "merit-based inheritance":

Our way of achieving this result is: covering method
Well, now we have to cover methods into line the following description :

(1) there exists only inheritance relationship between classes;
(2) sub-class method names name , the number of arguments and types , and the parent must be consistent covered;
(3) subclass return value must be covered parent consistent;
(4) a method of a subclass modifier can not "below" classification covered;
(5) if a violation of (2), is substantially overloaded methods, not covered

(I am here only to make students understand the code behind, only here On the cover, the follow-up blog post will cover methods for in-depth explanation of himself)

Now let's take a simple interesting example:
Let's build a package com.mec.about_override.demo, and the establishment of the following classes in the package:
Animal.java:

package com.mec.about_override.demo;

public class Animal {
    private String name;
    
    public Animal(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public void cry() {
        System.out.println("动物的叫声!");
    }
    
}

Dog.java:

package com.mec.about_override.demo;

public class Dog extends Animal {
    public Dog(String name) {
        super(name);
    }
    
    public void cry() {
        System.out.println("汪汪");
    }
}

Demo.java:

package com.mec.about_override.demo;

public class Demo {

    public static void main(String[] args) {
        Animal animal =new Animal("动物");
        animal.cry();

        Dog dog = new Dog("二愣子");
        dog.cry();
    }
}

We now compiled, the results are as follows:
Here Insert Picture DescriptionAs can be seen, we are in the Animal class written cry () method is covered!

Polymorphic:

Now, we have the knowledge to introduce another point of this post - polymorphism :

Polymorphism - one thing, at different times manifested in different states

首先,多态是有 条件的:

多态前提

  • 要有继承关系
  • 要有覆盖(方法重写)
    其实没有也是可以的,但是如果没有这个就没有意义
  • 要有父类引用指向子类对象
    形如:
    父 f = new 子();

接下来,本人来讲解一个非常有趣的 知识点:
基类 与 派生类 之间的 强制类型转换:
我们对上面的 Dog类 和 Demo类 做如下修改:
Dog.java:

package com.mec.about_override.demo;

public class Dog extends Animal {
    public Dog(String name) {
        super(name);
    }
    
    public void cry() {
        System.out.println("汪汪");
    }
    
    public void dogAction() {
        System.out.println("狗子快跑!");
    }
}

Demo.java:

package com.mec.about_override.demo;

public class Demo {

    public static void main(String[] args) {
        Animal animal =new Animal("动物");
        animal.cry();
        
        Dog dog = new Dog("二愣子");
        dog.cry();
        
        Animal otherAnimal = (Animal) dog;
        dog.dogAction();
        dog.cry();
    }
    
    
}

运行结果如下:
Here Insert Picture Description
现在可能就有同学有疑问了这里的输出结果竟然是“汪汪”而不是“动物的叫声”!

这里对上述问题做出解释:

基类 与 派生类 之间的 强制类型转换 遵循如下原则

  1. 对象的类型 约束 对象所能引用成员方法,但是,不能更改 成员 和 方法 的本质内容;
  2. 对于方法,强转不能改变 其所 实际指向 的 代码 的首地址; 对象 的类型 决定 对象 所能引用的 对象和方法 的种类(即:Animal类型); 对象 所能调用的 成员 和 方法 取决于所申请空间的 对象和方法(即:Dog类型);
  3. Subclass object may be converted into a strong parent class types, but the parent object can not be converted into subclasses strong type, since the type of the subclass members and methods may not be present in the parent class object

For the above phenomenon, probably students at the beginner would be the relationship between the base class and sub-class feeling a little dizzy.
Do not worry, here, I would also like to introduce a knowledge point, to assist us to identify the relationship between them:

Polymorphic member access features :

  • Member variable:
    Compile look left, look left to run.
  • Constructors:
    When you create a subclass of objects, will visit the parent class constructor, the data of the parent class is initialized.
  • Member method:
    Compile look left, look to the right to run.
  • Static methods:
    Compile look left, look left to run.
    (Static and class-related, not really rewrite, so the visit is to the left of)

So why the presence of this polymorphism mechanism?

Polymorphic benefits :

  1. Improve the maintenance of the code (inherited guarantee)
  2. Improved scalability code (guaranteed by the polymorphism)

So, all knowledge inherited explain it done, this blog is already finished the day 3:17 in the morning, and I was too sleepy to die, visible, conscience bloggers dedicated ah (ha ha, this little complain about, Wuguai).
If you have any doubts, comments or suggestions about the knowledge or code, in the comments area below raised, I will be answered as soon as possible, that help students leave little praise praise, thank you! ! !

Guess you like

Origin www.cnblogs.com/codderYouzg/p/12385960.html