The Artima Developer Community
Sponsored Link

Java Answers Forum
what is the need of Constructor in Interface

1 reply on 1 page. Most recent reply: Apr 25, 2012 11:06 PM by Marcos Luna

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
yobu Thanneeru

Posts: 1
Nickname: yobu
Registered: Dec, 2011

what is the need of Constructor in Interface Posted: Dec 16, 2011 1:44 AM
Reply to this message Reply
Advertisement
hi i am new to java
somebody plz help me


we cant create object for interface what is the need of Constructor in Interface.


Marcos Luna

Posts: 1
Nickname: marcosluna
Registered: Apr, 2012

Re: what is the need of Constructor in Interface Posted: Apr 25, 2012 11:06 PM
Reply to this message Reply
Kind of late but if it still helps someone.

An interface cant be instatiated, it just defines the methods a class must implement. So to be short if you have the interface

interface MyInterface {
void doSomething(int x);
}

the implementer class must be

class MyClass impmelents MyInterface {

public MyClass() {
/*
do what you want on contructor
or define additional constructors
*/
}

void doSomenthing(int x) {
//implement what you need here
}

//add aditional methos if wanted

}


Then you can do a new instance of MyClass with


MyClass obj1 = new MyClass();
//do something with obj1


And because the interface specify what an object can do, you also can do the following


MyInterface simpleObject2 = new MyClass();


You can assign a variable of type MyInterface with an object instatiated from the implementation class. just remember that this will only give you access to what the Inteface defines, sometimes it helps to manage simpler objects in your code.

Flat View: This topic has 1 reply on 1 page
Topic: WYSIWYG Html Editor with JEditorPane and HTMLEditorKit Previous Topic   Next Topic Topic: % in java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use