• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

who calls toString() when called on interface

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

==============================================================
public class Testing{ // line 1

public static void main(String[] args){ //line 2
List list = new ArrayList(); // line 3
System.out.println(list.toString()); // line 4
}
}
==============================================================
Why didnt line 4 throw compile time error as List interface doesn't have toString().
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is the ArrayList's toString() method that would be called.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class ArrayList gets the toString implementation from abstract class AbstractCollection.

Even otherwise all classes will get a default toString implementation from Object.
 
Suresh patel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply ,
I am aware that toString() of ArrayList will be invoked but
how does compiler know about it
lets take another example

public interface Shape{
public double area();
}

public class Square implements Shape{
double length=0;
public Square(double length) {
this.length = length ;
}
public double area(){
return length * length;
}
public void drawSquare(){
// do some thing
}
}

public class TestingShape{

public static void main(String[] args) {
Shape s = new Square ();
//------------------------------------------
s.drawSquare();
//Can I do this if No then how can I do
// list.toString()
//---------------------------------------------
}

}
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the Java Language Spec:

9.2 Interface Members

The members of an interface are:

  • Those members declared in the interface.
  • Those members inherited from direct superinterfaces.
  • 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.

  • [ December 07, 2007: Message edited by: Kelvin Lim ]
     
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    we can call only those methods on reference of super type (e.g. s in this case) those are defined or declared in it.
    i.e. toString is declared within List and defined(default implementation) in Object.
     
    Suresh patel
    Ranch Hand
    Posts: 32
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks you very much.
     
    Your mother was a hamster and your father was a tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!