Click to See Complete Forum and Search --> : Abstract and Interface
Deepa Natarajan
August 8th, 1999, 05:19 PM
Hi,
I have the code below.How do I implement in the main.
//// The code /////
interface B{
void SetColor();
}
abstract class C implements B{
int j =2;
int Set(int j){
System.out.println("abstract1");
return j;
}
}
class A extends C {
public static void main(String[] arg){
A a = new A();
a.Set(3);
a.SetColor(){
System.out.println("abstract2");
}
}
}
I am unable to implement the method from the interface.
Thanks,
Deepa
varbsjava
August 9th, 1999, 12:28 AM
Let me first explain what u mean by abstract class and an interface.
Abstract Class:
An abstract class cannot be instantiated. Only its subclasses can be instantiated. A method declared abstract is not actually implemented in the current class. It exists only to be overridden in subclasses. However, an abstract class is not required to have any abstract methods, though most of them do. Each subclass of an abstract class must override the abstract methods of its superclasses or itself be declared abstract.
Interface:
An interface is a collection of constants and abstract methods. It describes the public methods that a class implements and their calling conventions without saying anything about how those methods are implemented. It is
the responsibility of each class that implements an interface to provide code to handle the cases where the methods of the interface are called. Every Interface by default is abstract and private. And methods by default are abstract and public. All classes which implement the interfaces must provide the implementation of the methods in the interface or declare itself as abstract.
Coming to your code..
U have declared B as an interface and it has a method SetColor(). Class C which is declared abstract implements B. Class C doesn't have any implementation of SetColor(). So the class that extends C should provide the implementation for it.
In your case Class A extends C. But doesn't provide an implementation for SetColor(). So while compiling u will get can't instantiate Class A as it is abstract as mentioned above in the explanation. ie. It has to provide implementation for SetColor() method.
So in your class A provide an implementation for SetColor() method and proceed.
Hope this will explain u.
regards,
arun...
varbsjava
August 9th, 1999, 12:45 AM
Hi,
I haven't noted onething in your code. U have implemented SetColor() in your main as
a.SetColor(){
.......
}
U shouldn't do that. U have to implement the SetColor() method in the class not for the object of the class. This is not allowed in Java.
regards,
arun...
Deepa Natarajan
August 9th, 1999, 10:34 AM
Hi,
I am still having aproblem.For the following code it says ; expected in class A for SetColor().
What does it mean.
interface B{
void SetColor();
}
abstract class C implements B{
int j =2;
int Set(int j){
System.out.println("abstract1");
return j;
}
}
class A extends C {
public static void main(String[] arg){
A a = new A();
Set(3);
SetColor(){
System.out.println("abstract2");
}
}
}
Thanks,
Deepa
varbsjava
August 9th, 1999, 11:47 PM
I have mentioned about this in my other reply yesterday.
U have to implement the method in such a way that it should be an instance method of the class. But what u have done is u have implemented it in the main() method and also such type of method implementation is not allowed in Java. Java will expect a ; after the statement. So only it gives such an error. I think u would have got confused with anonymous class. Also the access modifier of the SetColor() method should public as per rules.
Another point to be noted u can't call or make a reference to non-static variable/methods from static methods as you did for calling Set() method from main(). U have to use the reference of the class if u want to access the non-static variables/methods from static methods. Or u have to make the method/variable static.
Just excute this. This will work.
interface B{
void SetColor();
}
abstract class C implements B{
int j =2;
int Set(int j){
System.out.println("abstract1");
return j;
}
}
class A extends C {
public static void main(String[] arg){
A a = new A();
a.Set(3);
a.SetColor();
}
public void SetColor(){
System.out.println("abstract2");
}
}
Khushhal
August 10th, 1999, 07:47 PM
Hi Deepa
You will have to define implementation of setColor() method in Class C since its implementing B.
The code should look like .
interface B{
void SetColor();
}
abstract class C implements B{
int j =2;
int Set(int j){
System.out.println("abstract1");
return j;
void SetColor()
{
// Implementation
}
}
}
class A extends C {
public static void main(String[] arg){
A a = new A();
Set(3);
SetColor(){
System.out.println("abstract2");
}
}
}
And if u like you can overirde it in Class A but you have to implement it in Class C else it will give you error.
Khushhal
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.