简体   繁体   中英

OOP Java. Error while trying to implement an abstract method

So, here is a piece of my abstract class:

abstract public void insert(Object obj);

And here is the implementation in a class that extends it:

public void insert(User u){
    try{
        String sql = "INSERT INTO " + TABLE + " (username, assword) VALUES ("+u.getUsername()+", " +u.getPassword()+")";
        conn.execSQL(sql);
    }catch(Exception e){
        System.out.println(e.getMessage());
    }finally{
        conn.close();
    }
}

and Here is the error:

The type --- must implement the inherited abstract method ---.insert(Object) userDAO.java

Basically, it is saying to me that I didn't implement any method that is called "insert" and receives an object as parameter. Doesn't my model "User" count as an object? What am I doing wrong here?

Thanks for your attention.

Ps.: error message on Eclipse while developing an Android App (I don't know if it changes anything).

Look at the method signatures,

void insert(Object obj);

and

void insert(User u)

are not the same, so you are not fulfilling the contract.

The method signature must match exactly . This includes the method name (ok), the return type (ok) and the parameter types (not ok.)

It doesn't matter that User must inherit from Object , they both explicitly need to be the same type. Think about it - otherwise someone could call your method with an Object which isn't a User , then the subclass would have no way of knowing how to deal with that parameter.

If you only want someone to call your method with User objects, and calling it with any other type would be an error, you may do something like this:

public void insert(Object o) {
    if(o instanceof User) {
        //Normal behaviour
    }
    else {
        throw new RuntimeException("Object must be of type user!");
    }
 }

Obviously the method signatures are not the same - the abstract method takes Object , but the implementation takes User .

However, if you want to implement such a pattern, try generics:

public abstract class MyAbstractClass<T> {

    abstract public void insert(T t);

}


public class MyImpl extends MyAbstractClass<User> {

    public void insert(User t) {
        // compiles OK
    }
}

Your method signature is incorrect, base and derived methods take different objects. Let me try and explain:

Assume you have a class Base which takes an instance of another class B1 in one of its methods (say m1). Also assume that you have a Derived class Derived1 which overrides the method (m1) in Base with the parameter D1 and similarly a derived class Derived2 which overrides the base method with parameter D2.

Both D1 and D2 implement/extend B1. This will be a problem because of the following scenario:

Base baseObj = new Derived1();
baseObj.m1(new D2()); // Should work if it is allowed to override a method in your way!

The above code snippet fails because the m1 method in Derived1 expects a parameter of type D1, but it got D2. Do you now see why this type of parameter casting is not permitted?

Note: It is a good practice to use @override annotation when overriding a method in base (from Java 1.5 specification)

The overriding method must have the header public void insert(Object obj) (example: try overriding the equals method of a class). You can always cast your argument within the method body.

That is because your abstract method insert has a parameter of type Object, which boils down to java.lang.Object and your attempt to implement the method has a User type supplied as a parameter. Your abstract method should use a generic type

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM