2

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 {

    }

3 Answers3

1

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";
    }


}
Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
  • Yes Pushpesh bro thanks for rectifying as a beginner hot confused a little, i also try to figure out , can i declare another name of method with same primitive type, like how we do get and pass variable, and i got cleared two doubt one is the method name should be same and we can leave implemented methods abstract class, hope can override also if i declared again names in subclass...? –  Oct 20 '18 at 07:07
  • Yes you can declare other methods with any name you want. Also, I would suggest to always use @Override annotation that will help you catch problems when unintentionally you make some typo in declaring the name. Let me update my answer to include that. – Pushpesh Kumar Rajwanshi Oct 20 '18 at 07:14
0

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

Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41
  • Bro yes, that is the only problem, i thought of declaring Double in another name but it should be same name which is declared as in interface and while trying confused a little . thanks bro –  Oct 20 '18 at 07:02
0

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;

  • Yes sister , i have fixed it, i though like variable names it would accept different name with type of Double, that is where i made mistake , thanks for your time sister. –  Oct 20 '18 at 12:50