Review and summary of Java Fundamentals < One > (2) Parent class reference to child class object (up, dynamic link)

Source: Internet
Author: User


Public class Animal {
Public static void Main (string[] args) {
Animal Animal = new Cat ();
Animal.voice ();
Drinkwater (animal); Call a static method, passing in a subclass object referenced by the parent class


}



String str;
Public void Voice () {
System.out.println ("Ordinary animal cries!") ");
}
Public static void Drinkwater (Animal Animal) {//static method, passed in Animal parent class object
System.out.println (Animal.getclass (). GetName () + "Drink water!");
}

}



class Cat extends Animal {
Public void Voice () {
System.out.println ("Meow meow Meow");
}
Public void Catchmouse () {
System.out.println ("Catch Mouse");
}


The declaration is the parent class, actually pointing to the subclass, the main idea is: polymorphic, dynamic link, upward transformation.

Upward transformation:

The subclass Object Cat is defined, thecat object inherits the Animal class, and theAnimal is the cat 's parent class. When we use aniaml cat = new Cat (); , a reference to aAnimal type can point to an object of type Cat.

subclasses are extensions to the parent class and generally have properties and methods that the parent class does not have in the child class. When we use an upward transformation, the definition of a reference to a subclass of a parent class object can use both the powerful function of the subclass object and the commonality of the parent class directly.

The advantage of this is that when we are dealing with large projects, we encounter a parent class that inherits multiple subclasses. And inevitably, quite a lot of static methods are used , and the parameters of the static method are best passed to a parent class object.

when we need to re-add a seed class, using an upward transformation, you can simply drop the subclass object into the static method without worrying about incompatibility. Otherwise do not use upward transformation, each occurrence of a new subclass, we have to invoke the existing method, we have to rewrite the method again, plus a new parameter class, so that the loss, reusability, and the code length is too long.

We also cannot throw the static method into the parent class, although doing so will allow subclasses to inherit the parent class directly regardless of the problem of upward transformation, but this will cause the parent class to be too bloated, which is the problem of design pattern.

Dynamic Links:

When a method of a parent class is only defined in the parent class and is not overridden in the subclass, it can be called by a reference to the parent class type, and for a method defined in the parent class, if the method is overridden in a subclass , the This method in the subclass will be called, which is the dynamic link.





Review and summary of Java Fundamentals < One > (2) Parent class reference to child class object (up, dynamic link)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.