Seventh talk about inheritance and interface hands-on experimentation and hands-on brain

Source: Internet
Author: User

"Hands-on experiment One"

Construction method invocation under inheritance conditions

    • Run the Testinherits.java sample, observe the output, notice that the call relationship between the parent class and the subclass constructs a method to modify the code of the parents constructor method, explicitly call the other constructor of grandparent, notice whether the calling code is the first sentence, the impact is significant!

Conclusion: By super calling the base class construction method, it must be the first statement in the subclass construction method.

Testinherits.java

Class Grandparent {

Public grandparent () {

System.out.println ("Grandparent Created.");

}

Public grandparent (String string) {

System.out.println ("Grandparent created.string:" + String);

}

}

Class Parent extends Grandparent {

Public Parent () {

Super ("hello.grandparent.");

System.out.println ("Parent Created");

Super ("hello.grandparent.");

}

}

Class Child extends Parent {

Public Child () {

System.out.println ("Child Created");

}

}

public class Testinherits {

public static void Main (String args[]) {

Child C = new Child ();

}

}

Experimental results:

"Hands on one's head"

Main points of the method override (override)

    • Method overrides require that the subclass be the same as the method of the parent class, otherwise the method overload (overload)!
    • Write your own code to test the following features (hands-on brain):
      • In subclasses, to invoke methods overridden in a parent class, you can use the Super keyword.

1 , Experiment code: Testinherit.java

Write your own code to test the following features (hands-on brain): in subclasses, to invoke the overridden method in the parent class, you can use the Super keyword.

Zhaoxuan Li,november 10th,2016.

Class Father

{

public void multiplication (double a,double b)

{

System.out.println ("Function of the parent class:" +a+ "*" +b+ "=" +a*b);

}

}

Class Me extends Father

{

public void multiplication (double a,double b)

{

Super.multiplication (a+1, b+2);

System.out.println ("Function of the parent class:" +a+ "*" +b+ "=" +a*b);

}

}

public class Testinherit

{

public static void Main (string[] args)

{

Me a=new Me ();

A.multiplication (1,1.2);

}

}

Results:

Seventh talk about inheritance and interface hands-on experimentation and hands-on brain

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.