Questions about the return value of a method that overrides a parent class in a Java class

Source: Internet
Author: User
Tags comparison table
questions about the return value of a method that overrides a parent class in a Java classIf you are in the parent class:
public class Person {
private String name;

Public String ShowName () {
return name;
}
}
The subclasses that override the ShowName method are:
public class Teacher extends person{

Public String ShowName () {
return name+ "Teacher";
}
}
Why this overlay method is not correct.
The return value of a subclass's override method must be written as
return Super.showname () + "teacher";
That's the right thing to do.  Put away the Quiet night rain | Browse 2,125 issues temporary shutdown recommended for 2016-08-14 04:51:34 best Answer
The method that overrides the parent class, that is, the override, and the subclass invokes the overridden method.

Override can be translated as overlay, literally knowing that it is overriding a method and rewriting it in order to achieve different roles. The most familiar overlay for us is the implementation of the interface method, in which the method is generally declared, and we need to implement all the methods of the interface declaration when we implement it. In addition to this typical usage, we may also overwrite methods in the parent class in the inheritance. The following points should be noted in the overlay:
1. The marking of the covering method must match the mark of the method covered, so as to reach the effect of coverage;
2. The return value of the overridden method must be consistent with the return of the overridden method;
3. The exception thrown by the overridden method must be the same as the exception thrown by the overridden method, or its subclass;
4, the overridden method cannot be private, otherwise only a new method is defined in its subclass, and it is not overwritten.
This answer by the computer network classification of talent Dong Hui recommended Comments (1) 0 9 release Comments 275408194 | 2016-11-19 15:50 return value: A subclass can be a compatible value reply for a parent class

There's a car 2015.

Adoption Rate: 85% from the team: mind-driven Excel: Computer/network Other answers are simple, in this method, you write name, the system does not automatically match Super.name but automatically match this.name, Since your name is not in this, the system cannot be found. You should be able to pass the written super.name. Ask

Thanks for answering, I would like to ask, since the private attribute subclass in the parent class cannot inherit, does the subclass have the name attribute in the problem.
Chase Answer
Owned, but not used. If you say so. An object: Person
p=new Teacher ("Blablabla");
Then the p.name can be used. You want to inherit the variable directly, you should use protected.
I copy a comparison table:
scope        Current class   same package      subclass     Other package public          √ 
√ Protected       √            √          x 
friendly        √             √                    x          x 
private         √             x                    x          x 

is also called default. is when you're not adding anything. Scope, that scope and Publick are similar, but do not support other package but support Java.lang.Object this package
  This answer was taken by the questioner timom    |  recommended on 2016-03-06 22:16:29 Review (2)   4  4 release Comments qjzh0603 | 2014-4-30 10:06 Written super.name is not passed, because name is the private property. Reply Enthusiastic Netizen | 2013-2-15 11:01 The teacher replied really well: the reply to the name in the parent class is private and can only be recognized by the parent class, and the private may be changed to public. youlian20    |  Posted on 2012-10-06 12:37 Comment   0  0 person's name is private and subclasses cannot be invoked directly. winters1224    |  Posted on 2012-01-21 15:39 Review   0  0 This topic has wood, I don't know hey. Corn Meatballs     |  released on 2012-01-21 15:38

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.