Manual brain 4 in Courseware

Source: Internet
Author: User
Tags c constructor

(1) Call of the relevant base class constructor during the inheritance process:

Before initializing the subclass object, the base class constructor is called and called sequentially according to the inheritance order. The reason is that the constructor is used for initialization, when initializing a subclass object, you must Initialize all the fields of its inherited base class, if you need to call the constructor of the parent class in the constructor of the subclass, the calling statement must be placed in the first sentence; otherwise, an error is reported.

The test code is as follows:

Public Class A {public a () {system. out. println ("the constructor of A is called") ;}} public class B extends a {public B () {super (); system. out. println ("B constructor called") ;}} public class C extends B {public C () {system. out. println ("C constructor called");} public class m {/*** @ Param ARGs */public static void main (string [] ARGs) {// method stub C child = new C () ;}} automatically generated by todo ();}}

Running result:

(1) implicit call of the plus sign: In the "+" operation, when an object is connected to a string object, the tostring () method is called implicitly, by default, this method returns "Class Name @ + hashcode ". To return meaningful information, subclass can override the tostring () method.

 

Public class main {/*** @ Param ARGs */Public String tostring () {return "OK";} public static void main (string [] ARGs) {// method stub automatically generated by todo main A = new main (); system. out. println ("A =" + );}}

 

(3 ):

Source code:

 

Public Class A {public void way () {system. out. println ("the way method of the parent class is called") ;}} public class B extends a {public void way () {super. way (); system. out. println ("the way method of the subclass is called");} public class c {/*** @ Param ARGs */public static void main (string [] ARGs) {// method of automatically generated todo stubs B k = new B (); K. way ();}}

Running result:

 

(4 ):

 

 

 

Source code:

 

Public class mammal {} public class dog extends mammal {} public class cat extends mammal {} public class m {/*** @ Param ARGs */public static void main (string [] ARGs) {// The method stub automatically generated by todo: mammal I = NULL; dog J = new dog (); cat k = new CAT (); I = J; j = I; j = (DOG) I; j = K; k = (CAT) I ;}}

The second and fourth sentences contain errors during compilation. The second sentence is because the subclass object can be directly assigned to the Base Class variable, however, when a base class object is assigned to a subclass object, forced type conversion is required. Similarly, when a subclass object is assigned to another subclass variable in the fourth sentence, forced type conversion is also required.

(5) source code:

 

public class ParentChildTest {    public static void main(String[] args) {        Parent parent=new Parent();        parent.printValue();        Child child=new Child();        child.printValue();                parent=child;        parent.printValue();                parent.myValue++;        parent.printValue();                ((Child)parent).myValue++;        parent.printValue();            }}class Parent{    public int myValue=100;    public void printValue() {        System.out.println("Parent.printValue(),myValue="+myValue);    }}class Child extends Parent{    public int myValue=200;    public void printValue() {        System.out.println("Child.printValue(),myValue="+myValue);    }}

 

Running result

 

 

A parent class and a subclass are created. The parent class and the subclass have the same variables and methods. The program assigns the subclass objects to the variables of the parent class, then the method of the variable is called. At this time, the sub-class is called. We can see that the object is of the Child type, and it calls the sub-type method, which is of the parent type, it calls the method of the parent type.

However, when a variable in the class is added with this variable, the result is not changed. Then, the forced conversion method is used to add one to the variable, and the result changes, we can see that the process of forced conversion is required to perform various operations on the variables in the subclass object called by the parent class variables.

Therefore, when the subclass value is assigned to the parent class variable, the field called by the parent class variable is the parent class. To use this variable to call the subclass field, it must be implemented through the forced conversion process.

Manual brain 4 in Courseware

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.