In [Java] inheritance, the parent class is overridden by the member variable, the accessibility of the method

Source: Internet
Author: User
Tags java se

In the Java inheritance mechanism, within the subclass, you can access variables and methods that are overridden by the parent class, and outside the subclass, you can access the overridden variables of the parent class, but you cannot access the overridden methods of the parent class.

Methods that are overridden in a parent class cannot be externally overridden by methods, which are due to encapsulation considerations.

Example:

Super, the parent class, has a member variable x, and the member Method DoSomething ().

Point, which inherits Super, but overrides Super X and DoSomething (), has its own member of the same name X and DoSomething ().

StaticDemo1, which demonstrates that in inheritance, internally, you can invoke a variable that is overridden by the parent class of a class, which can also be used externally.

StaticDemo2, which demonstrates that in inheritance, internally, you can invoke a method that is overridden by the parent class of a class, but it is not possible to do so externally.

Specific code:

Super, parent class

 Public class Super {    = "Super";      Public void dosomething () {        System.out.println ("dosomething in Super");}        }

Point, subclass, inherit Super

 Public classPointextendssuper{String x= "point";  Public voidPrintboth () {System.out.println (x); System.out.println (Super. x); }         Public voiddosomething () {System.out.println ("DoSomething in point"); }         Public voidDoboth () {dosomething (); Super. dosomething (); }}

StaticDemo1, demonstrating access to the member variables overridden by the parent class

 Public class StaticDemo1 {    publicstaticvoid  main () {        new  Point ();        Sp.printboth ();                        System.out.println (sp.x);        System.out.println ((Super) SP). x);}        }

The output is as visible as the member variables that are overridden by the parent class, whether inside the subclass or other third-party classes

Point  Super  Point  

StaticDemo2, demonstrates calling the method that the parent class is overridden

 Public class Staticdemo {    publicstaticvoid  main () {        new  Point ();            Sp.doboth ();
Sp.dosomething (); ((Super) SP). dosomething (); }}

The output is as follows: The overridden method of the parent class can be called inside a subclass, but the overridden method of the parent class cannot be called externally.

This is due to encapsulation considerations, to avoid external programs calling the overridden method of the parent class directly, and avoiding the subclass's new validation logic for the overridden method. Also for encapsulation purposes, the subclass can invoke the overridden method of the parent class, such as super.x, but cannot invoke the overridden method of the ancestor, such as super.super.x is not feasible

inch Point   inch Super   inch Point   inch Point

Resources:

Can Java Call Parent overridden method on other objects but not subtype? StackOverflow

8.3.1.1 static fields, the Java Language specification, Java SE 8 Edition

8.4.3.2 static Methods, the Java Language specification, Java SE 8 Edition

In [Java] inheritance, the parent class is overridden by the member variable, the accessibility of the method

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.