-2

I have following class

public class RegisterUserDetails extends DatabaseCall {
public String setAddUserQuery(User user){
  //code

  return query;
 }
}

And here is the DatabaseCall class

public class DatabaseCall {
public ResultSet executeQuery(String query){
//code
return rs;
}

}

I was trying to program to a supertype/interface by doing this

DatabaseCall dbCall = new RegisterUserDetails();

However I cannot view the setAddUserQuery method this way. Why is this? It seems programming to an interface is only applicable for abstract classes and interfaces?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
user3809938
  • 1,114
  • 3
  • 16
  • 35
  • 1
    It's the slicing problem: The DatabaseCall API does not include a method called setAddUserQuery. Only RegisterUserDetails has that method. You can either move that method into the super class OR cast the reference. – duffymo Apr 03 '17 at 23:02
  • 2
    Programming to an interface will fail with abstract classes and interfaces if you add public methods that are not part of the parent's contract and make them required for use. This has nothing to do with what the parent of the child happens to be. – Hovercraft Full Of Eels Apr 03 '17 at 23:03
  • *"Why is this?"* - Because `DatabaseCall` doesn't provide any methods called `setAddUserQuery` - this is polymorphism in action, the `RegisterUserDetails` is capable of been used as `DatabaseCall` as it extends `DatabaseCall`, but in doing so, it can only perform the operations that `DatabaseCall` defines – MadProgrammer Apr 03 '17 at 23:03
  • Is this a duplicate of [*What does it mean to “program to an interface”?*](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) Do the answers there answer this question? – T.J. Crowder Apr 03 '17 at 23:08
  • Hint: it is not exactly good practice to put up such an unclear question. And to then just walk away, and leave the people who try to help you without any feedback ... doesn't improve the situation. I am *really* surprised that I am the first one to close-request this. – GhostCat Apr 04 '17 at 07:09
  • I wouldn't say it was "such an unclear" question, i went into details and gave example code and as a result I got the answer I wanted. I do appreciate it was a long time, but things happen. – user3809938 Apr 04 '17 at 16:02

1 Answers1

0

However I cannot view the setAddUserQuery method this way. Why is this?

Because DatabaseCall doesn't have setAdduserQuery. The interface you have to the object doesn't have the method.

It seems programming to an interface is only applicable for abstract classes and interfaces?

No, it's not at all limited to that. But the interface you have (defined by interface or class) must have the features you need. In your case, DatabaseCall doesn't have the feature (setAddUserQuery) that you need.

It may be that you should add it to DatabaseCall, or that the specific code that needs to use setAddUserQuery needs to be written to a more feature-rich interface (one that includes setAddUserQuery; perhaps RegisterUserDetails).

But the goal of programming to an interface is to program to the features required, rather than a concrete type. The interface (features required) still has to have all of the features you need. For instance, you program to List rather than ArrayList, because List has all the features you need, so there's no need to work with the concrete type, and later you may find it's useful to use LinkedList instead.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875