Java learns from scratch (inheritance), and java inherits from scratch

Source: Internet
Author: User

Java learns from scratch (inheritance), and java inherits from scratch
I. Inheritance

Inheritance and reuse of previous code is very easy. It can greatly shorten the development cycle, reduce development costs, and increase program maintainability.

Inheritance is A way to enable duplicate Class A to directly use the attributes and methods of another class B.

Class A can have its own attributes and Methods

Ii. Inheritance implementation class inheritance format use the extends keyword in Java to complete the class inheritance relationship. Operation format: class parent class {} // defines the parent class subclass ExtendsParent class {} // use the extends keyword to implement inheritance
Package com. pb. person;/** parent class */public class Person {private String name; // name private String sex; // gender private int age; // age // set getter and setter Methods public String getName () {return name;} public void setName (String name) {this. name = name;} public String getSex () {return sex;} public void setSex (String sex) {if (sex. equals ("male") | sex. equals ("female") {// sets the Gender limit. If the value is invalid, an error is returned. sex = sex;} else {System. out. println ("invalid gender, Gender: male or female") ;}} public int getAge () {return age;} public void setAge (int age) {if (age> 0 & age <= 150) {// set the age limit this. age = age;} else {System. out. println ("the age must be between 1 and") ;}} public Person () {this. name = "anonymous"; this. sex = "male"; this. age = 22;} public Person (String name, String sex, int age) {this. name = name; this. sex = sex; this. age = age;} public void say () {System. out. println ("self-introduction:"); System. out. println ("name:" + this. name); System. out. println ("Gender:" + this. sex); System. out. println ("Age:" + this. age );}}

Subclass:

Package com. pb. person;/** subclass */public class Student extends Person {private int stuNo; // Student ID private String specialty; // professional public Student () {} public Student (String name, string sex, int age, int stuNo, String specialty) {super (name, sex, age); this. stuNo = stuNo; this. specialty = specialty;} // getter/setter method public int getStuNo () {return stuNo;} public void setStuNo (int stuNo) {this. stuNo = stuNo;} public String getSpecialty () {return specialty;} public void setSpecialty (String specialty) {this. specialty = specialty ;}}

Test class

Package com. pb. persontest; import com. pb. person. student; public class Test {public static void main (String [] args) {Student stu = new Student ("James", "male", 22,121, "Computer Science and Technology"); stu. say ();}}

Result:

Self-Introduction:
Name: James
Gender: male
Age: 22

The following shows that the student ID and major of the subclass are not output,

The say () method of the parent class called by default.

Method that requires subclass to override the parent class

Iii. Method Rewriting

Method rewriting is the process in which subclass occurs to the parent class. It is in two classes, and method Overloading is in a class.

Method rewriting has the following requirements:

The Student class overrides the say () method in the Person class.

Package com. pb. person;/** subclass */public class Student extends Person {private int stuNo; // Student ID private String specialty; // professional public Student () {} public Student (String name, string sex, int age, int stuNo, String specialty) {super (name, sex, age); this. stuNo = stuNo; this. specialty = specialty;} // method Override @ Override public void say () {super. say (); System. out. println ("student ID:" + this. stuNo); System. out. println ("Major:" + this. specialty);} // getter/setter method public int getStuNo () {return stuNo;} public void setStuNo (int stuNo) {this. stuNo = stuNo;} public String getSpecialty () {return specialty;} public void setSpecialty (String specialty) {this. specialty = specialty ;}}

Test class unchanged:

Package com. pb. persontest; import com. pb. person. student; public class Test {public static void main (String [] args) {Student stu = new Student ("James", "male", 22,121, "Computer Science and Technology"); stu. say ();}}

Result:

Self-Introduction:
Name: James
Gender: male
Age: 22
Student ID is: 121
Major: Computer Science and Technology

After rewriting, the method after the subclass rewriting is called.

4. super keywords

Super must appear in the subclass (in the subclass method and constructor), rather than in other positions. It must appear in the first line of the constructor.

Used to access members of the parent class, such as the attributes, methods, and constructor of the parent class.

Access permission restriction. For example, the private member cannot be accessed through super.

5. Call rules for Constructor methods under inheritance Conditions

Rule 1:If the constructor of the subclass does not call the constructor of the parent class through the super display, and other constructor of the parent class is not explicitly called through this, by default, the system first calls the no-argument constructor of the parent class.

Rule 2:If the super constructor of the subclass shows that the constructor of the parent class is called, the corresponding constructor of the parent class is executed, without the constructor of the parent class being executed.

Rule 3: If the subclass constructor uses this to explicitly call other constructor, apply the above two rules to the constructor.

5.1. Example 1:
Package compb. demo4;/** parent class */public class StaticSuper {public static String staticGet () {return "parent static method";} public String dynamicGet () {return "parent dynamic method" ;}} package compb. demo4;/** subclass */public class StaticSub extends StaticSuper {// The static method cannot override public static String staticGet () {return "subclass static method ";} @ Override public String dynamicGet () {return "subclass dynamic method Override parent dynamic method" ;}} package compb. demo4;/** Test */public class Test {public static void main (String [] args) {StaticSub s = new StaticSub (); // declare the subclass object System. out. println (s. staticGet (); // call the static method System of the subclass. out. println (s. dynamicGet (); // call the dynamic method of subclass }}

Result:

Static subclass Method
Subclass dynamic method override parent Dynamic Method

5.2. Example 2,
Package com. pb. demo5;/** parent class */public class VenderLate {public VenderLate () {System. out. print ("VenderLate out") ;}} package com. pb. demo5; public class MoveGoods extends VenderLate {public MoveGoods () {System. out. println ("MoveGoods out! ");} Public MoveGoods (String s) {System. out. println (s) ;}} package com. pb. demo5;/** Test class */public class Test {public static void main (String [] args) {MoveGoods m = new MoveGoods (); moveGoods g = new MoveGoods ("child ");}}

Result:

VenderLate outMoveGoods out!
VenderLate out child

5.3. Example 3
Package com. pb. demo4;/** parent class */public class SuperClass {private String name; public SuperClass () {System. out. println ("No parameter constructor for the parent class");} public SuperClass (String name) {System. out. println ("the parent class has a constructor parameter:" + name) ;}// setter/getter method public String getName () {return name ;} public void setName (String name) {this. name = name ;}}

Subclass

Package com. pb. demo4; public class SubClass extends SuperClass {private String name; private int count; public SubClass () {System. out. println ("SubClass construction method without Parameters");} public SubClass (int count) {System. out. println ("SubClass has a parameter constructor:" + count);} // call the constructor public SubClass (String name) {super (name); this. name = name; System. out. println ("the call subclass has a parameter constructor and uses super to display the call" + name);} // setter/getter method public String getName () {return name ;} public void setName (String name) {this. name = name;} public int getCount () {return count;} public void setCount (int count) {this. count = count ;}}

Test class:

Package com. pb. demo4; public class Test {public static void main (String [] args) {System. out. println ("************"); SubClass s1 = new SubClass (); System. out. println ("******** the call subclass has a parameter constructor method but does not use super to display the call **********"); subClass s2 = new SubClass (34); System. out. println ("******** a parameter constructor is called to call **********"); subClass s3 = new SubClass ("James ");}}

Result:

* ******* Call the non-parameter constructor of the subclass ********* the non-parameter constructor of the parent class does not have a parameter constructor ***** ** the call subclass has a parameter constructor but does not use super to display the call ********** the non-parameter constructor of the parent class has a parameter constructor: 34 ******* a parameter constructor is used to call the sub-class. At the same time, the super display is used to call the **********. The parent class has a parameter constructor: the sub-class of zhangsan call has a parameter constructor. At the same time, the super display is used to call zhangsan.
Vi. Object Class

All classes automatically inherit the Object class in the java. lang package.

The Object class is the parent class (base class) of all classes)

If the extends keyword is not used when defining a class, the Object class is inherited by default.

Methods that are often overwritten by objects:

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.