Madalina Gheboianu
Madalina Gheboianu

Reputation: 23

Error in java at adding methods

I read that you can add extra methods for an object, or override the abstract methods in this way, yet I have an error. Can you tell me if this is even possible or what I have done wrong?

public abstract class Pesti {
    public abstract void ud();
}

public class EX2 {
    public static void main(String[] args) {
        Pesti p = new Pesti() {
            public void ud() {
                System.out.println("Pestele este ud!");
            }

            public void inn() {
                System.out.println("Innoata!");
            }
        };
        p.ud();
        p.inn();
    }
}

It tells me that it cannot find method inn.

Upvotes: 1

Views: 66

Answers (4)

bcsb1001
bcsb1001

Reputation: 2907

Pesti is an abstract class with exactly one method (which is abstract): ud(). As such, a variable declared with the type Pesti can only be used to call ud() or any method from the class Object (Pesti's implicit superclass). p is declared with this type, so despite actually being an instance of an anonymous class with the method inn(), this method is not available in the context of having a variable of declared type Pesti.

Perhaps you intended to add the inn() method to Pesti so all instances must implement it:

public abstract class Pesti {
    public abstract void ud();
    public abstract void inn();
}

Or perhaps you don't really want to use anonymous classes at all and want to create a subclass of Pesti with the inn() method available:

public class PestiImpl extends Pesti {
    @Override
    public void ud() {
        System.out.println("Pestele este ud!");
    }

    public void inn() {
        System.out.println("Innoata!");
    }
}

(or you could have a layer of inheritance between the two; some abstract class extending Pesti with inn() declared as an abstract method, if you have other classes like this with the inn() method but some Pestis without).

Side note: I strongly recommend using the @Override annotation where appropriate to make your code more readable and to avoid mistakes like typos when trying to override a method. You can get most IDEs to insert it automatically.

Upvotes: 0

Eran
Eran

Reputation: 393821

What you are doing is creating an anonymous sub-class of your Pesti. However, since your p variable is of type Pesti, you can only call methods declared in the Pesti class (or any of its super-classes). Therefore you can call ud(), but not inn().

If you could cast p to the type of the sub-class, you could have called inn(), but since this is an anonymous sub-class instance, you can't cast to that type.

You could declare inn() as an abstract (or concrete) method in Pesti. Then your code would pass compilation, and the implementation of this method in the anonymous sub-class instance will be executed.

public abstract class Pesti {
    public abstract void ud();
    public abstract void inn();
}

Upvotes: 6

Sven D.
Sven D.

Reputation: 23

You declared p as an instance of Pesti, but tried to use a method not defined in Pesti. When you declare your object as the abstract class you can only use the methods defined in this class. To use your inn() method you have to declare this method in the Pesti class as well. Alternativly you can write a class extending Pesti which implements the inn() method and declare your object as the extending class.

Upvotes: 1

Hayk Petrosyan
Hayk Petrosyan

Reputation: 373

Think you need to look at this link, I don't know what do you want to do , but you can't instantiate abstract class, you can find detailed description in my link, hope it'll help you

Upvotes: -1

Related Questions