Related articles:
The output of a Java program | Java exercises 】 【 first (resolution)
Difficulty level: Easy
Predict the output of the following Java program.
Considering that if you post the answer immediately after the question, students may accidentally see the result before thinking, so I put the question and the answer separately, with something in the middle, I hope it will not bring you difficulty in reading
The problem
Problem a:
package main;
class Base {
public void Print(a)
{
System.out.println("Base"); }}class Derived extends Base {
public void Print(a)
{
System.out.println("Derived"); }}class Main {
public static void DoPrint(Base o)
{
o.Print();
}
public static void main(String[] args)
{
Base x = new Base();
Base y = new Derived();
Derived z = newDerived(); DoPrint(x); DoPrint(y); DoPrint(z); }}Copy the code
Click here to skip to the answer
Problem two:
package main;
// filename Main.java
class Point {
protected int x, y;
public Point(int _x, int _y)
{ x = _x; y = _y; }}public class Main {
public static void main(String args[])
{
Point p = new Point();
System.out.println("x = " + p.x + ", y = "+ p.y); }}Copy the code
Click here to skip to the answer
Put a picture of a cute girl to relieve eye fatigue, the second half of the article is the output and analysis of the program
Answer to question one
Output:
Base
Derived
Derived
Copy the code
Predicting the first line of output is easy. We create an object of type Base and call DoPrint(). DoPrint calls the print function, and we get the first line.
DoPrint(y) results in the second line of output. As in C++, it is allowed in Java to assign derived class references to base class references. Therefore, the expression Base y = new Derived() is a valid statement in Java. In DoPrint(), o starts referencing the same object that y references. Also, unlike C++, functions in Java are virtual by default. Therefore, when we call o.prinint (), the Derived class’s print() method is called because of the default runtime polymorphism in Java.
DoPrint(z) results in the third line of output, where we pass a reference to the Derived type and call the Derived class’s print() method again. One thing to note here is that unlike C++, object slicing does not occur in Java. Because non-primitive types are always allocated by reference.
Answer to question two
The output
Compiler Error
Copy the code
In the above program, there is no access problem because Point and Main are in the same package, and the protected members of one class can be accessed from other classes in the same package. The problem with this code is that there is no default constructor in Point.
As with C++, the Java compiler does not create a default constructor if we write our own parameterized constructor. Therefore, you made the following two changes to the Point class to fix the program.
Delete the parameterized constructor. Add a constructor that takes no arguments. Java does not support default arguments, so this is not an option.
The related content
The output of a Java program | Java exercises 】 【 first (resolution)
The output of a Java program | Java exercises 】 【 a second (resolution)
The output of a Java program | Java exercises 】 【 a third (resolution)
The output of a Java program | Java exercises 】 【 4 sets (including parsing)
I’ve been writing a technology blog for a long time, and this is one of my Java worksheets. Hope you like it! More related articles and my contact information I put here:
Github.com/wanghao221 gitee.com/haiyongcsdn…
If you do learn something new from this post, like it, bookmark it and share it with your friends. 🤗 Finally, don’t forget ❤ or 📑 for support