Java starts from scratch and the basics of learning notes & lt; heavy load _ overwrite _ inheritance & gt; (10)

Source: Internet
Author: User

When the override (override) subclass inherits the parent class, all methods in the parent class are inherited by the quilt class (except the private method ), if the methods in the parent class cannot meet the requirements of the subclass, the method of the parent class must be overwritten in the subclass. The following conditions must be met at the same time: * When a subclass overwrites the method of the parent class, the return value type of the method and the parameter of the method name must be consistent with that of the parent class * override the method of the parent class in the subclass, the method modifier type cannot be stricter than the method modifier type of the parent class. For example, the method modifier in the parent class is protected, then the access modifier of your subclass that overwrites the method is either protected or public *. Before the method, there is a @ Override identifier Eg: parent class: package com. ibm. overrideMethod1; publicclass Person {// This is the parent class public String read (String book) {return "The read book is" + boo K;} public StringeatEgg (String name, int num) {return name + "eat" + num + "" ;}} subclass: package com. ibm. overrideMethod1; publicclass XiaoBai extends Person {@ Override public String read (String book) {return "" + super. read (book );} // you can also call the read method in the class. // The method in the parent class cannot meet the requirements of the subclass. // overwrite the publicstaticvoid main (String [] args) {XiaoBai xb = new XiaoBai (); String ss = xb. read ("One Thousand and One Nights"); System. out. println (ss );} Result: the book that Tom is reading is a one thousand and one-night Summary: The subclass covers the methods in the parent class. The subclass method is consistent with the parent class method, only the operation to be done by the method is changed, that is, the method is rewritten. In a class, all methods have the same method name, but only the list of parameters of the method. Different return value types have different return value types, the number of parameters you pass must be of the same type as the return value. The number of parameters in the 1 parameter list is different from that in the public void run () {// automobile run method} // the return value type must be the same as public void run (String name) {} public void run (Stringname, String pass) {} 2 different types of parameters passed are the same public void run (String name) {} public void run (int I) {} Eg: publicclass Student {// print the Student information publicvoid show () {System. out. println ("this is a student");} public void show (String nam E) {System. out. println ("Student name" + name);} public void show (String name, int age) {System. out. println ("Student name" + name + "student age" + age);} public void show (String name, int age, boolean flag) {if (flag = true) {System. out. println ("Student name" + name + "age" + age + "exam passed");} else {System. out. println ("Student name" + name + "age" + age + "failed exam");} syntax of the reload constructor: Syntax: access Modifier + class name (parameter list) {} by default, each class has a non-parameter constructor and a constructor with parameters (the parameter can be one or two or more) if a class contains Parameters If the constructor with parameters overwrites the default parameter-free constructor, what we call is a constructor with parameters defining parameters as different types of constructor. In a class, can I initialize different types of values through the constructor? Eg: publicclass NewManyObject {doubled; longl; // defines four different types of attributes inti; String s; // This is the default parameter-free constructor 1 public NewManyObject () {} // initialize different types of attributes. 2 public NewManyObject (double d) {this. d = d; System. out. println (this. d);} // 3 public NewManyObject (long l) {this. l = l;} // 4 public NewManyObject (int I) {this. I = I;} // 5 public NewManyObject (String s) {this. s = s;} publicstaticvoid main (String [] args) {// double type attribute Initialize the No. 2 constructor NewManyObject mo = newNewManyObject (12.5d);} Summary: The constructor is overloaded, but the type and number of parameters passed in the constructor are different, the class names of constructors are the same. When you create an object, you can call the corresponding constructor to call the constructor when initializing any attributes: use the this keyword to call the corresponding constructor publicclass NewManyObject01 {private String name; private String pass; private String age; private String tel; // pass a parameter to the constructor, initialize the name attribute 1 public NewManyObject01 (String name) {this. name = name;} // 2 public NewManyObject01 (String name, String pass) {// this. name = name; // In this constructor, this (name) is called; // this indicates that the constructor passing a parameter is called. pass = pass;} // 3 public NewManyObject01 (String name, Stringpass, String age) {this (name, pass); // indicates calling the constructor 2 this. age = age ;}} when Super and this keyword Super keyword subclass inherit the parent class: in the subclass, you can call the attributes and methods in the parent class by hitting the super keyword. You can also call the constructor subclass of the parent class: package com. ibm. supers; publicclass SonSuper extends FatherSuper {publicvoid show () {// you can access the attributes in the parent class using the super keyword. // However, if the attributes in the parent class are modified using private, super cannot access super. f = 12.5f; super. name = "admin"; // a class inherits a parent class and can directly access the method super in the parent class. find (); super. select () ;}// if the parent class has a method with the same name as the subclass, then I want to access the method publicvoid find () {System. out. println ("My subclass method");} public SonSuper () {// access the constructor of the parent class in the constructor of the subclass. // only the constructor of the parent class can be transferred to the constructor of the parent class, however, the property super ("cw");} publicstaticvoid main (String [] args) {new SonSuper (). show () ;}} parent class: package com. ibm. supers; publicclass FatherSuper extends GrandFatherSuper {privateintid; public String name; protectedfloatf; publicvoid find () {System. out. println (name);} public String fingName () {returnname;} public FatherSuper () {} public FatherSuper (String _ name) {System. out. println ("parent class constructed value:" + _ name) ;}} output result: parent class constructed value: cwadmin passes the current object as a parameter to other methods or constructor package com. ibm. thises; publicclass Teacher {publicvoid show (Teacher t) {System. out. println (t);} publicvoid show1 () {show (this); // This is an object of the current class, student st = new Student (this) created by the default no-argument constructor has been called ); // pass the object of the current class as a parameter to the constructor} publicstaticvoid main (String [] args) {new Teacher (). show1 (); // System. out. println (new Teacher () ;}} package com. ibm. thises; publicclass Student {private Teacher t; public Student (Teacher t) {this. t = t ;}} free block: Also known as free block * No matter which constructor is used to create an object, the free block is first executed, and then the object package com. ibm. news; publicclass Test {privateintid; public Test (int id) {System. out. println ("------ I am the constructor ----"); this. id = id;} {System. out. println ("------- I am a non-static initialization block ------"); this. id = 15; // call the show ();} publicvoid show () {System. out. println (this. id);} publicstaticvoid main (String [] args) {Test tt = new Test (20); System. out. println (tt. id) ;}} output result: ------- I am a non-static initialization block ------ 15 ------ I am the constructor ---- 20 = and equals = are used to determine whether the values pointed to by two referenced objects or simple data types are equals determines whether the content of the two objects is equal.

Related Article

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.