Inheritance and overrides in Java

Source: Internet
Author: User

Inheritance in Java is a subclass of methods and properties that inherit from the parent class (except private properties and methods), and there is no inheritance for the parent class's private properties and method subclasses, but for the subclass to have access to the private properties of the parent class, the private property must be given a method interface that is accessed externally.

Package com. Pengrong; Public classDemo { Public Static void Main(string[] args) {/* Pupil stu1 = new pupil ();  Stu1.pay (500f); Stu1.  ShowName ();  SYSTEM.OUT.PRINTLN ("Tuition for primary school students" + Stu1.getfee ());  Student AK1 = new Student ();  Ak1.pay (888.0f);  SYSTEM.OUT.PRINTLN ("Tuition fee for middle school students" + Ak1.getfee ()); */  //multi-state Implementation modeStu STU1 =NewPupil (); Stu1.pay ( -f); Stu1.  ShowName (); System. out. println ("Tuition for primary school students"+ Stu1.getfee ()); STU1 =NewStudent (); Stu1.pay ( $f); System. out. println (Stu1.showname ()); System. out. println (Stu1.gettestv ()); }}//Student Class parent classClass stu{//define member properties  PublicString name; Public intAge Public floatFeePrivate intTestv = -; PublicStringShowName() {return  This. Name; }//Member Method  Public void ShowName() {System. out. println ("Student Name"+ This. name); } Public float Getfee() {returnFee } Public void Setfee(floatFee) { This. fee = fee; } Public void  Pay(floatFee) {System. out. println ("Default payment Method"); This. fee =0.5F*fee; } Public int Gettestv() {returnTestv; } Public void Settestv(intTESTV) {Testv = Testv;}}//Pupil class sub-categoryClass Pupil extends stu{ Public void  Pay(floatFee) {System. out. println ("How to pay for primary school students"); This. fee =fee; }}//middle School classClass Student extends stu{ Public void  Pay(floatFee) {System. out. println ("How to pay for Chinese students"); This. fee =0.8f * FEE; } PublicStringShowName() {return  This. Name; }}

For example, there is a parent class Stu contains an attribute private int testv;
Subclass student inherits the parent class Stu,
Then write the code inside the Main method

Stu stu1 = new Student();System.out.println(stu1.TestV);

This is wrong, the error is said Testv This property is not visible, although the Stu parent class reference to a subclass of the object is possible, but the parent class reference is not to a parent class Stu object entity, the parent class refers to the object entity type is student So this STU1 reference cannot refer to the private property of the parent class, what if you want to access the private properties of the parent class? One solution is to provide a way to access private properties within the parent class, and this method cannot be private.
The rules that the Java platform executes are: "In the compilation phase, you can invoke those methods and access those properties, which are determined by the reference type, and when the program runs, the method is executed, and access to that property is determined by the type of the object." ”

The essence of coverage is to let the methods in the subclass mask the methods of the parent class
Overlay grammar Note two.
1, the child class overrides the parent class method, then the subclass method in the method return type, the method name, the parameter list should be consistent;
2. The subclass method cannot narrow the access rights of the parent class method
Or the class above, the parent class Stu has a pay method, and the subclass overrides the method, which overrides the pay method in the subclass implementation code.

Stu stu1 =new Pupil();stu1.pay(500fnew Student();stu1.pay(200f);

By calling this code, you can see that the pay method is called through the parent class reference, and instead of executing the parent class, he executes the pay method that is overridden in the subclass. The execution flow for a method that overrides a parent class in a subclass is as follows
1, such as in the compiler compile routines, the compiler processing to Stu1.pay (500F); Time, found that Stul is a reference to Stu, and Stu inside there is exactly a pay method, so compiled through.
2, program run time, the program runs to Stu1.pay (500f), the time, found that stu1 point to the object is student class object, so he first to student class in accordance with the method of pay method signature to find pay method, if found then execute this method, Go to the parent class without finding it until you find it.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Inheritance and overrides in Java

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.