The following program when run will product stackoverflow as output.
I want to know what is happening in the line where a TestA is being instantiated.
interface TestA { String toString(); }
class ...
|
Can we instantiate Interface? If yes why if no why? I am a beginner.Please explain elaborately.Please explain with suitable code?
|
hi whenever you have an interface specified as return value then you do not receive an instance of that interface but you receive an instance of a class that implements this interface. This is the same as with parameters: when your method expects a Connection you can pass everithing that implements this interface. did that help ? pascal |
|
Hi Syamsul, No. That creates an anonymous class. It's similar to this: class AnonymousClass implements ActionListener { public void actionPerformed(ActionEvent e) { Graphics g = getGraphics(); g.setColor(getBackground()); g.fillRect(0, 0, getSize().width, getSize().height); } } Button b = new Button("button"); b.addActionListener(new AnonymousClass()); Only the class created can only be instantiated in the b.addActionListener() call. If you look in the directory where you compile ... |
Hi All, Before we start, I have searched on JavaRanch and googled on this topic and it just confused me even more. So I'm sorry if the answer was out there and it passed me by. With ref to the following from Head 1st Java chap12: import javax.sound.midi.*; . . some stuff . Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); . . . ... |
Head First Java 2nd edition, on page 232, pool puzzle (page 234 puzzle solution). My understanding of an abstract 'thing', is that it can never be instantiated. All interfaces are implicitly public abstract. An array is an object '= new' means to instantiate an object Please can someone explain how one can say the following when Nose is an interface? Nose[] ... |
|
|
|
You can only "instantiate an interface" if you supply implementations for all its methods. What actually gets instantiated is an anonymous class that happens to implement this interface. If you compile this code, you'll see that there's an extra class file being created called something like "Test$1.class" (assuming that this code is part of the Test class) - Test$1 is the ... |
|
hi all, below is a piece of code.it says we can instantiate the interface and it works fine too..can somebody clarify about this. interface sporty{ void besporty(); } class ferrari implements sporty{ public void besporty(){ System.out.println("in concrete class"); } } class testsporty{ public static void main(String args[]){ sporty[] sportythings = new sporty[2]; sportythings [0]= new ferrari(); sportythings[1]= new ferrari(); } } ... |
Equitus wrote: Thank you jverd. You explanation is clear. I had guessed that some kind of implicit instantiation of another object must be underneath this construction. I speculated that it might be a copy of its outer class, with the new instance implementing the interface. You have called it object X. Is this an instance of Object? Every instance of every ... |
sarcasteak wrote: I guess I am just not sure what the benefit of casting an object into an interface is. I know the declarations of interface methods are blank, by casting do all these blank methods get filled by the object being casted? Nope. They're "filled in" by the class that implements the interface. That's what implementing an interface means. The ... |
Hi can some body explain what is happening at line 10 in below code interface Foo { int bar(); } public class Sprite { public int fubar( Foo foo) { System.out.println(foo.bar()); return foo.bar(); } public void testFoo() { fubar(new Foo() { public int bar(){return 1; } }); } } how can we instantiate an interface using "new" |
|
An interface is like an outline, where the class is the actual essay. The interface mentions the things that need to happen. A better way to remember it might be if you are already familiar with the terms User Interface, Graphical User Interface, and GUI. Suppose you had a closed metal box with a button on the outside, but you knew ... |