JAVA learning-object-oriented Feature 2: inheritance, java object-oriented

Source: Internet
Author: User

JAVA learning-object-oriented Feature 2: inheritance, java object-oriented

* 1. Object-oriented Feature 2: Inheritance
 

* 1. Why design inheritance?
*
* 2. Implement class inheritance through the "class A extends B" class.
* Subclass: A parent class (or base class SuperClass): B
*
* 3. After the subclass inherits the parent class, the subclass can obtain the attributes and Methods declared in the parent class.
* Clear: when the parent class has a private property or method, the subclass can also be obtained, but because of the encapsulation design, the subclass cannot be directly obtained.
* Call.
* In addition to inheritance, sub-classes can get the structure of the parent class and define their own unique components.
*
* Extends: The subclass is an extension of the parent class function. It is clear that the subclass is not a subset of the parent class.
*
* 4. Inheritance of classes in java only supports single inheritance: one class can only inherit one parent class. Conversely, a parent class can have multiple child classes.
* 5. The child parent class is a relative concept.
*
* 2. Method rewriting --- (method overloading) modifier return value type method name (parameter list ){}
* 1. premise: a subclass inherits the parent class.
* 2. After the subclass inherits the parent class, if the method of the parent class is not applicable to the subclass, The subclass can overwrite, overwrite, and overwrite the method of the parent class.
* 3. Rewrite Rules: 1) requires that the "Return Value Type method name (parameter list)" of the subclass method be the same as that of the parent class.
* 2) the modifier of the subclass method cannot be smaller than that of the parent method.
* 3) * If the parent class method throws an exception, the exception type thrown by the subclass method cannot be greater than that of the parent class.
* 4) * The Sub-parent class methods must be both static and non-static.
1 public class TestExtends {2 public static void main (String [] args) {3 Student s = new Student (); 4 s. eat (); 5 6 Worker w = new Worker (); 7 w. eat (); 8 9 Person p = new Person (); 10 p. eat (); 11 12 s. setAge (12); 13 s. setName ("Ma hualong"); 14 System. out. println (s. getName () + ":" + s. getAge (); 15 16 Graduate g = new Graduate (); 17g. eat (); 18 g.info (); 19g. show (); 20} 21} 22 23 class Person {24 priva Te String name; 25 private int age; 26 27 public String getName () {28 return name; 29} 30 31 public void setName (String name) {32 this. name = name; 33} 34 35 public int getAge () {36 return age; 37} 38 39 public void setAge (int age) {40 this. age = age; 41} 42 43 public void eat () {44 System. out. println ("dinner"); 45} 46 47 void walk () {48 System. out. println ("Walking"); 49} 50 private void sleep () {51 52} 53} 54 55 class A {56 57} 58 59 class Student extends Person {60 // class Student extends Person, A {61 // private String name; 62 // private int age; 63 private String schoolName; 64 65 public Student () {66 67} 68 public Student (String name, int age) {69 // this. name = name; 70 // this. age = age; 71 this. setName (name); 72 this. setAge (age); 73} 74 75 // public String getName () {76 // Return name; 77 //} 78 // public void setName (String name) {79 // this. name = name; 80 //} 81 // public int getAge () {82 // return age; 83 //} 84 // public void setAge (int age) {85 // this. age = age; 86 //} 87 // rewrite the method with the same name as the parent class, overwrite 89 public void eat () {90 System. out. println ("more nutritious"); 91} 92 public void walk () {93 System. out. println ("walking without a schoolbag"); 94} 95 public void info () {96 eat (); 97 // System. out. println ("name:" + name); 98 System. out. println ("I am a student"); 99} 100 // It is not a rewriting of the parent class's private sleep () method. 101 private int sleep () {102 return 0; 103} 104 105} 106 107 108 109 class Worker extends Person {110 public void eat () {111 System. out. println ("worker dinner"); 112} 113 114 public void walk () {115 116} 117} 118 119 class Graduate extends Student {120 public void show () {121 System. out. println ("I am a graduate student"); 122} 123}

 

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.