Alex
Alex

Reputation: 69

Do I need to declare methods of abstract class in respective interface?

In my application I have activities of 2 types, Hotel and Restaurant which I have implemented as abstract classes. I need to call a method which is on the HotelClass but the IDE tells me to create a method in abstractActivity. If I do this, it will create a duplicate method. How can I resolve this?

package pt;

public class HotelClass extends AbstractActivity {

    private int starClass;
    private int price; //preço medio por noite

    // Basic Account Constructor
    public HotelClass(String city, String name, String description, String address, int starClass, int price, String owner) {
        super(city, name, description, address, owner); // chamada construtor super-class
        this.starClass = starClass;
        this.price = price;
    }

    public int getStar(){
        return this.starClass;
    }

    public int getPrice(){
        return this.price;
    }

}

My abstract class and super class below interface

Upvotes: 1

Views: 267

Answers (2)

nakosspy
nakosspy

Reputation: 3942

class AbstractActivity is not abstract. This is why the IDE is asking you to implement those methods. If you declare it as abstract, then you won't have this problem.

public abstract class AbstractActivity implements ...

To make things clearer:

The abstract class AbstractActivity is a base class that will not be instantiated, it is there in order to put functionality that is common in the concrete classes HotelClass and RestaurantClass. An abstract class does not need to have implemented all the methods declared by the interaces it implements. But a concrete class must provide implementation to all methods of the interfaces it implements. This is why AbstractActivity does not need to have implementation of all the methods declared by the interfaces it implements. But the concrete classes (the ones that are instantiated) need to know how they will implement the interface, so they need implementation for all methods. Some method implementations could be provided by their superclass.

Upvotes: 2

Mik378
Mik378

Reputation: 22171

Your class AbstractActivity is not declared as abstract.

Therefore, you are forced to implement all interface methods from top-hierarchy directly in this class.

You just have to declare as:

public abstract AbstractActivity implements ...

Upvotes: 1

Related Questions