Differences between inheritance and Java scope public, private, and protected

Source: Internet
Author: User

Inheritance:
That is, subclass inherits and extends the features of the parent class.

I have three classes:
Grandfather father son
The relationship is Level 1 Inheritance Level 1.

There is a common method in it:
Public void tellstory ()

Directly run the Code:
Grandfather class:
Package demo;

Public class grandfather {

Protected void tellstory (){
System. Out. println ("your grandfather and I ......");
}

}

Father class:
Package demo;

Public class father extends grandfather {

Protected void tellstory (){
System. Out. println ("your father and I ......");
}

}

Son class:
Package demo;

Public class son extends father {

Public void tellstory (){
System. Out. println ("My ......");
}

}

The first test class:
Package demo;

Public class test {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
Son son = new son ();
Son. tellstory ();
}

}

The second test class:
Package test;

Import demo. Father;
Import demo. Son;

Public class test {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){

Son son = new son ();
Son. tellstory ();

}

}

1. Subclass can only enlarge the visible permission of the parent class method, but cannot narrow down the permission.
For example:
Tellstory () is public in the grandpa class, so the father class cannot be protected or private.

2. Public Private protected:
Public: can be called anywhere
PRIVATE: can only be called in this class
Protected: Used in the subclass or in the same package.

The following is better:

Http://blog.csdn.net/ladofwind/archive/2006/06/05/774072.aspx

Before explaining the Four keywords, I want to make a simple definition of the relationship between classes. For a class that inherits its own, the base class can think that they are all their children, classes in the same directory as yourself are considered to be your friends.

1. Public: indicates that the data member and member function are open to all users and can be directly called by all users.

2. Private: Private indicates private. Private means that no one except the class itself can directly use private property. Private property cannot be infringed, even if it is a child or friend, none.

3. Protected: for children and friends, protected is public and can be used freely without any restrictions. For other external classes, protected becomes private.

 

 

Scope: current class, same package, Child class, other package

Public √

Protected √ ×

Friendly √ ××

Private √ ×××

The default value is friendly when no data is written.

3. How can I directly reference the methods of Grandpa class in the Sun Tzu class of 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.