Java-Inheritance-practice Highlights

Source: Internet
Author: User

1. What is the output of the following code?
ublic class Test {    public static void main(String[] args)  {        new Circle();    }} class Draw {         public Draw(String type) {        System.out.println(type+" draw constructor");    }} class Shape {    private Draw draw = new Draw("shape");         public Shape(){        System.out.println("shape constructor");    }} class Circle extends Shape {    private Draw draw = new Draw("circle");    public Circle() {        System.out.println("circle constructor");    }}

This topic focuses on the sequence of calls and initialization of constructors at class inheritance. One point to remember is that the constructor invocation of the parent class and the initialization process must precede the child class. Because the Circle class's parent class is a shape class, the Shape class initializes first and then executes the constructor of the shape class. Then the sub-class circle is initialized, and the last circle's constructor is executed.

2. What is the output of the following code?
public class Test {    public static void main(String[] args)  {        Shape shape = new Circle();        System.out.println(shape.name);        shape.printType();        shape.printName();    }} class Shape {    public String name = "shape";         public Shape(){        System.out.println("shape constructor");    }         public void printType() {        System.out.println("this is shape");    }         public static void printName() {        System.out.println("shape");    }} class Circle extends Shape {    public String name = "circle";         public Circle() {        System.out.println("circle constructor");    }         public void printType() {        System.out.println("this is circle");    }         public static void printName() {        System.out.println("circle");    }}

This problem mainly examines the difference between hiding and covering.

Overrides are only for non-static methods (the end-state method cannot be inherited, so there is a overwrite), and the shadowing is for member variables and static methods. The difference between the 2 is that the overlay is constrained by the RTTI (Runtime type identification) and is hidden from the constraint. This means that only the overlay method will be dynamically bound, and the shadowing will not occur dynamically. In Java, all other methods, except the static method and the final method, are dynamically bound. As a result, the above output will appear.

Summarize

The constructor invocation of the parent class and the initialization process must precede the child class
The order of initialization is also in accordance with; Object member Initialization---> Normal object code block initialization---constructor method initialization

Java-Inheritance-practice Highlights

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.