Package and access permission 2, access permission 2

Source: Internet
Author: User

Package and access permission 2, access permission 2

Write a Father class in Package a with attributes: Age (private), name (public );

Features: Work (public), drive (public ).

Compile a Child class Son in Package a with attributes: Age (protected) and name;

Features: Play (private), Learning (public ).

Finally, write the main class Test in Package B and Test the class Father and class Son in the main method of the main class.

Father:

 1 package pack1; 2  3 public class Father { 4     private int age; 5  6     public int getAge() { 7         return age; 8     } 9 10     public void setAge(int age) {11         this.age = age;12     }13 14     public String name;15 16     public void work() {17         System.out.println(name + ": I have a job.");18     }19 20     public void drive() {21         System.out.println(name + ": I have a car, I'm driving.");22     }23 }

Son:

 1 package pack1; 2  3 public class Son { 4  5     protected int age; 6  7     public void setAge(int age) { 8         this.age = age; 9     }10 11     public int getAge() {12         return age;13     }14 15     private String name;16 17     public void setName(String name) {18         this.name = name;19     }20 21     public String getName() {22         return name;  23     }24 25     private int play() {26         System.out.println(name + ": I like to play !");27         return age;28     }29     public int getPlay(){30         return age;31     }32 33     public void study() {34         System.out.println(name + ": I like to study !");35     }36 }

Text:

1 package pack2; 2 3 import pack1.Father; 4 import pack1.Son; 5 6 public class Text_F {7 8 public static void main (String [] args) {9 Father f1 = new Father (); 10 f1.setAge (55); 11 f1.name = "James"; 12 13 f1.drive (); 14 f1.work (); 15 int a = f1.getAge (); 16 System. out. println (f1.name + "Age:" + a + "years old. "); 17 18 Son s1 = new Son (); 19 s1.setAge (17); 20 s1.setName (" Zhang sansan "); 21 s1.study (); 22 int B = s1.getAge (); 23 System. out. println (s1.getName () + "Age:" + B + "years old. "); 24 25} 26 27}

Result:

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.