2

I have an abstract class

public abstract class SuperclassA{
   public abstract void method(SuperclassB b);
}

And an implementation

public class SubclassA extends SuperclassA{

   @override
   public void method(SubclassB b){
   }
}

where SubclassB extends SuperclassB.

My NetBeans editor complains that I am not overriding method. What can I do to actually pass a subclass to method?

k88074
  • 2,042
  • 5
  • 29
  • 43

5 Answers5

6

In order to override a method of the base class, the sub-class's method must have the same signature.

If you want the overridden method to accept only instances of SubclassB, you can test for it :

public class SubclassA extends SuperclassA
{
   @Override
   public void method(SuperclassB b){
       if (!(b instanceof SubclassB)) {
           // throw some exception
       }
       SubclassB sb = (SubclassB) b;
       ...
   }
}

You can call that method as follows :

SuperclassA a = new SubclassA ();
a.method (new SubclassB ());
Eran
  • 387,369
  • 54
  • 702
  • 768
  • With it be OK to use the following? Consider `A` an object of `SubclassA`: `A.method(new SubclassB);` Otherwise, how can I possibly pass an instance of `SuperclassB`, since it is `abstract`? – k88074 Jun 01 '16 at 13:22
  • @ZzKr Yes, you can, assuming A is an instance of SubclassA . – Eran Jun 01 '16 at 13:25
  • @ZzKr See https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html An Instance child object can also be passed on as its patents type. Meaning you can use it in any situation where the parent class (up to Object) is called for. – Akunosh Jun 01 '16 at 13:36
1

Think of the method signatures of a class or interface as a contract or a promise of what it and any subclass can do.

SuperclassA promises to do action method for any SuperclassB. In order for SubclassA to be a SuperclassA it must do action method for all SuperclassB. The issue is that it only does action method for some types of SuperclassB.

Here is the same situation with names that are easier to understand.

abstract class Pet {
    // Pet methods
}

class Cat extends Pet {
    // Cat methods
}

class Dog extends Pet {
    // Dog methods
}

abstract class PetShop {
    public abstract void groom(Pet pet);
}

The Pet shop class promises that any PetShop can groom any Pet, including dogs.

PetShop shop = new FranksPetShop();
shop.groom(new Dog());

If we define FranksPetShop like so it only defines how to groom Cat's, any other type of Pet (including dogs) is not defined.

class FranksPetShop {
    @Override
    public abstract void groom(Cat cat) {
        // Groom that cat
    }
}

To solve this FranksPetShop has to define how to groom a Pet not just a Cat.

class FranksPetShop {
    @Override
    public abstract void groom(Pet pet) {
        // Groom that pet
    }
}
cyroxis
  • 3,661
  • 22
  • 37
0

Actual, Method Signature is,

public abstract void method(SuperclassB b);// into abstract class

Your overrided Method Signature must be same, Otherwise it's not override, in fact it will be treated like overloading,

@override
   public void method(SuperclassB b){
   }

  //OverLoad not Override
   public void method(SubclassB b){
   }
Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55
0

The method in SubclassA takes in a type of SubclassB. The method in SuperclassA is taking in a type of SuperclassB. You are not overriding the parent class method, you are overloading by using a different type.

Depending on what you are trying to accomplish, you could try this

public class SubclassA extends SuperclassA{

   @override
   public void method(SuperclassB b){
   }
}

Which would override the method you want. As long as SubclassB is a child of SuperclassB, then you will still be able to pass SubclassB in.

You can still have an overloaded method that takes in Subclasss if you want.

public class SubclassA extends SuperclassA{

   @override
   public void method(SuperclassB b){
   }

   public void method(SubclassB b){
   }
}

Take a look at this thread for more info about overloading vs overriding.

Community
  • 1
  • 1
0

Refer to oracle documentation page on override.

An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.

The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed.

The overriding method has the same name, number and type of parameters, and return type as the method that it override

In your case, you did not use same method signature.

public void method(SubclassB b){

should be changed to

public void method(SuperclassB b){

Now you can use workaround suggested by Eran by using instanceof

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211