Understanding the inheritance of three major features of Java

Source: Internet
Author: User

Learning Source: http://www.cnblogs.com/chenssy/p/3354884.html

Defaults default permissions (package permissions-the same package can be accessed)

Private (the class can be used internally, and inherited subclasses cannot access it)

Protected (classes and packages can be accessed internally, and their subclasses can also be accessed)

Public any can access

Permission adornments for class can be used only with public and default

Inherited
Implementing the inheritance mechanism of a class using the extends keyword in Java
By inheritance, subclasses automatically have all members of the base class (superclass)
Java only supports single inheritance and does not allow multiple inheritance

classperson{PrivateString name; Private intAge ;  Public voidsetName (String name) { This. Name =name; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicString GetName () {returnname; }     Public intGetage () {returnAge ; }}classStudentextendsPerson {PrivateString School;  Public voidSetschool (String school) { This. School =School; }     PublicString Getschool () {returnSchool; }}/*** * Implementation of class inheritance mechanism using extends keyword in Java * through inheritance, subclasses automatically have all members of the base class (superclass) * Java only supports single inheritance and does not allow multiple inheritance*/ Public classExtendtest { Public Static voidMain (string[] args) {Student Student=NewStudent (); Student.setname ("Jone"); Student.setage (18); Student.setschool ("SCH");        System.out.println (Student.getname ());        System.out.println (Student.getage ());    System.out.println (Student.getschool ()); }    }

Understanding the inheritance of three major features 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.