Reputation:
I am trying to extends an abstract class which is implemented one method of interface so in my subclass i am trying to implement rest of the methods declared in interface but sub class forcing me to declare all the methods of interface, please help me to fix this, thanks in advance i have added my code below. Thanks much in advance seniors.
My code
interface xxx
{
int numbers();
String names();
Double salary();
}
abstract class GetNames implements xxx
{
public String names()
{
return "Ravi";
}
}
class Check extends GetNames
//This class forcing me to implement names also
{
public int numbers()
{
return 3;
}
public double sal()
{
return 25000.00;
}
}
public class AbsUsingInterface {
}
Upvotes: 2
Views: 1458
Reputation: 18357
You only need to implement methods from Interface which have not been implemented in abstract class which is a super class for your class where you are trying to implement methods.
But looks like I see one problem in your Check class.
Your interface declares this method,
Double salary();
Where as in check class you are implementing this method,
public double sal()
So this really doesn't implement a method from interface. You need to make it same as it is in interface.
Just make method in your Check class like this,
public Double salary()
{
return 25000.00;
}
While implementing/overriding a method from superclass/interface, you should always use @Override annotation so in case any of your method signature differs, it will prompt you for error right there. And yes if you declare names() method again in your subclass Check, it will override the one in abstract class.You can do something like this in your class,
abstract class GetNames implements xxx
{
@Override
public String names()
{
return "Ravi";
}
}
class Check extends GetNames
{
@Override
public int numbers()
{
return 3;
}
public double sal()
{
return 25000.00;
}
@Override
public Double salary() {
return sal();
}
@Override
public String names() { // this overrides names() method in GetNames class
return "Check";
}
}
Upvotes: 1
Reputation: 11
your GetNames class is implementing xxx interface but you are only implementing names() you must implement salary method.
interface xxx
{
int numbers();
String names();
Double salary();
}
abstract class GetNames implements xxx
{
public String names()
{
return "Ravi";
}
public Double salary()
{
return null;//just return null;
}
}
class Check extends GetNames
{
public int numbers()
{
return 3;
}
public double sal()
{
return 25000.00;
}
}
or just throw NotImplementedException;
Upvotes: 0
Reputation: 3600
A concrete class extending an abstract
class must provide body to all the abstract method in super class.
Methods in an interface are by default- abstract
unless you provide a default body.
When an abstract class implements an interface, all those methods of an interface are inherited as it is i.e. abstract
Now for your scenario, where you have provided a body for one of the inherited method of the interface, this method is no longer abstract in the scope of 'Abstract' class. So, if a class extends this abstract class, then it need not provide a body for the above method because it is no longer abstract(They can of-course override it).
You are getting an error in Check
subclass that you have defined for not inheriting salary()
method, not names()
method that you have already defined in GetNames
abstract class
Upvotes: 0