Java-package access permission 3 (27th sets .)

Source: Internet
Author: User

The following is a java4android video tutorial from marschen.

Main content of this set.
1. Access Permissions and inheritance.
2. Protected permission

Package COM. marsdroid; // if the child class and the parent class are not in the same package, the Child class cannot inherit the default permission of the parent class. // member variables and member functions. (problematic) // if the child class and the parent class are not in the same package, the Child class can inherit the member variables with the default permission in the parent class. // and the member functions, however, permissions are insufficient and cannot be used. // If the subclass and parent class are in the same package, // student // void introduce () {// system. out. println ("My name is" + name ", my age is" + age); //} // executable // defines public, you can access the/protected permission across packages. The permission is the same as that of default, but protected can only modify member variables and member functions. // But default can modify the class. // use member variables and functions for the protected cross-package. public> protected> default> privatepublic class person {public string name; // you can access protected int age across packages. // you can only access the cross-package of the quilt class. // example: in the program, it can only be accessed by the student subclass. Age // cannot be accessed by the class test. because it is not a subclass of // person, there is no relationship between person and test. // If the variable is declared as private, the variable can only be used in this class. void eat () {system. out. println ("eat");} void sleep () {system. out. println ("Sleep ");}}

Package Org. marsdroid; import COM. marsdroid. person; class student extends person {void introduce () {system. out. println ("My name is" + name + ", my age is" + age );}}

package org.marsdroid;import com.marsdroid.Person;class Test{public static void main(String args[]){Person  p  = new Person();p.name = "zhangsan";p.age = 10;}}


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.