| |

VerySource

 Forgot password?
 Register
Search
View: 1852|Reply: 13

The toString() problem is handled in the magic interface! !

[Copy link]

1

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-9-28 19:30:01
| Show all posts |Read mode
Today I suddenly discovered such a problem that there seems to be a default toString() method in the interface, like the following example:
interface test
{

}
public class TesttoString implements test
{
private String s;
public TesttoString(String str){
s = str;
}
public String toString(){
return s;
}
public String returnS(){
return s;
}
public static void main(String[] args){
test o = new TesttoString("I don't know!!");
System.out.println(o);
System.out.println(o.toString());
System.out.println(((TesttoString)o).returnS());
//System.out.println(o.returnS());

}
}
I did not define the toString() method in the interface, but the class that implements TesttoString can be upcast and paid to test. If the subclass inherits from the parent class, this is easy to understand. The classes are all descendants of Object, but I don’t seem to be there. I saw in this book that the interface has this function! Like o.returnS() this call will report an error, this is easy to understand, but it is toString()! ! ! !
Reply

Use magic Report

1

Threads

20

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-9-28 19:45:01
| Show all posts
toString() is the default method to load classes in the java.lang package
Reply

Use magic Report

0

Threads

14

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 Invalid IP Address

Post time: 2020-9-29 10:15:01
| Show all posts
interface test has access to method toString(), this method is from class Object.

In class TesttoString, toString() method is overridden. When the program is running, the overridding toString() method in class TesttoString is called based on the object type instead of variable reference type.
Reply

Use magic Report

0

Threads

14

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 Invalid IP Address

Post time: 2020-9-29 10:30:01
| Show all posts
interface test has access to method toString(), this method is from class Object.

This toString() method is overridden in class TesttoString. When the program is running, the overridding toString() method in class TesttoString is called based on the object type instead of variable reference type.
Reply

Use magic Report

1

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-9-30 00:15:01
| Show all posts
Does it mean that the interface also has toString by default?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-30 09:15:01
| Show all posts
The toString method is defined in the Object class! Of course all classes will have this method!
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-30 09:30:01
| Show all posts
All classes in Java inherit from the Object class by default. Even if you do not explicitly inherit the Object class when writing the class, the Java compiler will implicitly add
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-30 10:30:02
| Show all posts
This phenomenon is indeed very interesting. The above few people seem to have not noticed a problem, that is, the research object here is like an "interface" rather than a "class", and the interface seems to not inherit the Object class theoretically.

My understanding of this phenomenon is that the processing of the base class Object in JAVA is another way
Not "all classes inherit the Object class, so all objects have its methods"
But "All objects have the methods of the Object class by default, which just seem to conform to the rule of inheritance"

Haha, let's study together
Reply

Use magic Report

0

Threads

7

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-9-30 11:15:01
| Show all posts
Sun's official document TJLS (The Java Language Specification)! Among them, Chapter 9 Section 9.2 has such a paragraph about the interface:

If an interface has no direct superinterfaces, then the interface implicitly
declares a public abstract member method m with signature s, return type r,
and throws clause t corresponding to each public instance method m with
signature s, return type r, and throws clause t declared in Object, unless a
method with the same signature, same return type, and a compatible throws
clause is explicitly declared by the interface. It is a compile-time error if the
interface explicitly declares such a method m in the case where m is declared to
be final in Object.


The interface implicitly defines a set of methods that are exactly the same as the method signatures in the Object class; if the interface defines one that is defined as final in the Object, a compilation error will occur.
Reply

Use magic Report

1

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-9-30 11:45:01
| Show all posts
The interface seems to really imply a set of methods in Object. I did the following hashCode() test
interface test
{

}
public class TesttoString implements test
{
private String s;
private int i;
public TesttoString(String str,int j){
s = str;
i = j;
}
public String toString(){
return i+": "+ s;
}
public String returnS(){
return s;
}
public int hashCode(){
return i;
}
public static void main(String[] args){
test o = new TesttoString("I don't know!!",1);
System.out.println(o);
System.out.println(o.toString());
System.out.println(((TesttoString)o).returnS());
System.out.println(o.hashCode());
//System.out.println(o.returnS());

}
}

The result can be output of Hashima 1
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list