user3460494
user3460494

Reputation:

How to use Abstract Class/ Interface correctly?

I have a problem with my abstract class.

Here is my interface:

package dovilesUzduotis4;

import java.util.ArrayList;

public interface Interface1 {
    void a(ArrayList<K> kM, String g);
}

and abstract class:

package dovilesUzduotis4;

import java.util.ArrayList;

public abstract class Service implements Interface1 {
    public void iK(ArrayList<Ks> kM, String g){
        K aK = new K(g);
        kM.add(aK); 
    }
}

But when I try to use service.iK(kM,g); in main I get the error "service cannot be resolved". How can I correct that?

Upvotes: 0

Views: 158

Answers (4)

TTT
TTT

Reputation: 2012

Your specific example seems to be related to some sort of webservice api. Without the backing code to the abstract class we can't really help you there.

I think we can start with some simple fundamentals related to interfaces and abstract classes, since that seems to be your question.

Abstract classes are classes that you cannot, by definition, create an instance of. What darijan did to "construct" and instance of the abstract class is he is creating an anonymous inner class for the Service abstract type.

Service service= new Service() { }; // notice the curly braces, this is an anonymous class definition

There are many different schools of thought and opinions related to best practices with abstract classes and interfaces. What we really are talking about here is the heart of OOP, in my opinion. Abstract classes are meant to provide APIs with or without concrete implementation, so that they may be overridden and specialized for a specific purpose.

This would be a decent example:

public class Car {
String make;
public car (String make) { this.make = make; }
}

public class Hondacar extends Car{
public Hondacar() { super("honda"); }
}

Now you have the definition of what states define a "Car" object, and then you specialize that into the definition of a "Hondacar".

Hopefully this makes sense.

Onto interfaces... Interfaces are declarations of a public API. They are a "contract" that implementing classes must abide by. A class that implements an interface must, by definition, implement all methods on that interface. YOU CAN THINK of an interface as an abstract class with only abstract methods, where classes that subclass that abstract class will need to override every method on that supertype(this draws parallels to the "@override" annotation on implemented interface methods) though many will probably discourage this way of thought. I am not sure what you are trying to do with your specific example since it does not have any names that I can even draw inference from so I can't really help you there.

So drawing on the whole car example, a similar design would be:

interface Car { 
String getMake();
}

class HondaCar implements Car {
private static final make = "honda";
@override
public String getMake() { return HondaCar.make; }
}

You can see how the interface does not provide any sort of implementation at all, it merely defines the public API that an implementing class must offer.

Upvotes: 0

Gaskoin
Gaskoin

Reputation: 2485

This topic is suitable only for deletion.

ArrayList operates on Ks type, and you guys are putting inside it an K type object... You should extend this class, or make it not abstract (by implementing interface) if you want to instantiate it.

Upvotes: 0

sybear
sybear

Reputation: 7784

First of all, Java is case-sensitive meaning that service and Service are different things. The error you just got: service cannot be resolved says, that service class is expected, while you have Service class.

Remember, that class names should implement the CamelCase, while variable names should start with a small letter.

To call methods you must either:

  • Create an object and access its method
  • Or make the method static

In the first case, you have to implement a child class:

SubService extends Service {}

because service is abstract and is expected to be extended.

Then:

SubService service = new SubService();
service.method();

In the second case, you do not have to extend the class, static methods can be called.

public abstract class Service implements Interface1 {
    public static void iK(ArrayList<Ks> kM, String g){ //static method
        K aK = new K(g);
        kM.add(aK); 
    }
}

Then:

Service.iK(arg0, arg1);

Upvotes: 0

darijan
darijan

Reputation: 9775

Please paste in the main method first.

My guess is you forgot to instantiate an object of the class:

Service service= new Service() { //create an object of the class
}; //brackets are there because the Service is abstract class and I am redefining it.
service.iK(kM, g); //invoke a method an that object

Now, I don't think that the Service class needs to be abstract. You render the class abstract if you expect a user to implement a method (or methods) of that class that is marked as abstract in a manner that suits his needs. Needless to say, I don't see any abstract method in your Service class.

So it comes to this:

  • if the class is NOT abstract, you instantiate it as:

    Service service= new Service();

  • if the class is abstract, you must redefine it at place:

    Service service= new Service() {

    //here you could implement an abstract method or redefine an existing one

    };

Upvotes: 1

Related Questions