user10100235
user10100235

Reputation:

Do i need to implement all the methods of interface again while inheriting abstract class in Java?

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

Answers (3)

Pushpesh Kumar Rajwanshi
Pushpesh Kumar Rajwanshi

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

Pallavi Bachute
Pallavi Bachute

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

Ashishkumar Singh
Ashishkumar Singh

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

Related Questions