Subclass inherits the parent class, overrides the parent class method, invokes the overridden method when using polymorphism, calls the subclass, or calls the parent class?

Source: Internet
Author: User

Package fifth day; public class Person {public void say () {System.out.println ("I am the Say method for person");}}

The subclass student is as follows:

The fifth day of the package, public class Student extends person{//overrides the parent class's say () method @overridepublic void Say () {System.out.println (" I am student's say Method ");} public static void Main (string[] args) {Student stu=new Student (); Stu.say ();//I am Student say method person p=new Student ();// Polymorphic P.say ();//I am the Say Method of Student}}
the above P.say () can be understood as if the subclass is called by the Say () method, and if the subclass does not have a say () method, then the Say () method of the parent class is called again.

The purpose of inheritance is to extend (meaning that the function of the parent class is not powerful, we can extend the parent class), and thus the code is reused.

Student extends Person

Derived class extension base class------------->> foreign names

Subclass inherits Parent class-->> domestic term



Subclass inherits the parent class, overrides the parent class method, invokes the overridden method when using polymorphism, calls the subclass, or calls the parent class?

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.