Java Classroom questions and answers 5

Source: Internet
Author: User

One: Run the Testinherits.java sample, observe the output, summarize.

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 ();
}

}

Summarize:

Calling the base class construction method through super must be the first statement in the subclass construction method. Otherwise you will get an error. The results of the operation are as follows:

Why do I have to call the construction method of the parent class before the constructor of the subclass is run? Can you turn around? Why can't it be reversed?

For:

Constructs an object that first calls its constructor method to initialize its member functions and member variables.
Subclasses have all the variables and methods of the parent class, and the parent does not have some of the variables and methods of the child class.
If not called, the member variables and member methods inherited from the parent class are not properly initialized.
This can not be called in turn, because the parent class does not know that the child is a divine demon variable and that the subclass is not initialized with the parent class variable, causing the program to run an error!

Second, the direct output of an immutable class of objects, what will be the result?

Sample program source code:

Class a{}

public class Explorationjdksource {

public static void Main (string[] args) {
System.out.println (New A ());
}

}

Run Result: type name [email protected]+ random number

Reason:

The main method actually calls the Ancestor class function: public void println (Object x), which internally invokes the ValueOf method of the String class.

The Object.ToString method is also called inside the ValueOf method:

The Hashcode () method is a local method, implemented by the JVM Designer: public native int hashcode ();

Therefore, the class name [email protected]+ hash value is displayed by the result of the layer call.

Third, we look at the Fruit.java code, to summarize the reasons.

Fruit.java

public class Fruit
{
Public String toString ()
{
Return "Fruit toString.";
}

public static void Main (String args[])
{
Fruit f=new Fruit ();
System.out.println ("f=" +f);
System.out.println ("f=" +f.tostring ());
}
}

Operation result :

Summarize:

The fruit class overrides the ToString method of the object class.

In the "+" operation, when any object is connected to a string object, its ToString () method is called implicitly, and by default, this method returns "class name @ + Hashcode". To return meaningful information, subclasses can override the ToString () method.

Four

There are now three classes:

Class mammal{}

Class Dog extends Mammal {}

Class Cat extends mammal{}

Three variables are defined and initialized for each class

Mammal M=null;

Dog D=new Dog ();

Cat c=new Cat ();

Which of the following statements will cause a compilation error? Which one will cause a run-time error? Why?

M=d;

D=m;

D= (Dog) m;

D=c;

C= (Cat) m;

A: Program Run results

The second line and the fourth line are faulted.

When you assign a parent class to a subclass, you need to force conversions, and the subclass is assigned to the parent class without forcing conversions. The two subclasses of a parent class must also be forced to convert when they are assigned to each other.

Please see the "Perverted" category below. What is the result of the program running? How would you explain that you would get such an output? The computer is not wrong, the reason why you get such a result is also a reason, then from these running results, you can summarize the Java syntax features?

Program Source code:

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);
}
}
public class Fruit
{
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 ();

}
}

Operation Result:

Summarize:

When a subclass has the same method as the parent class, and a parent class variable refers to a subclass object, which method is called, determined by the object's own "true" type, that is, the object is a subtype, it calls the method of the subtype, it is the parent type, and it invokes the method of the parent type. This feature is actually a concrete representation of the object-oriented "polymorphic" feature.

If the child class has the same field as the parent class, the fields in the subclass are substituted or hidden from the fields of the parent class, and the fields in the subclass are accessed (not the fields in the parent class). If the subclass method does want to access a field that is hidden in the parent class with the same name, it can be accessed with the Super keyword. If the subclass is used as a parent class, the field accessed through the subclass is the parent class!

Java Classroom questions and answers 5

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.