coder25
coder25

Reputation: 2393

overriding method in interface and classes

 interface A { public void m1(); }
 **//Gives error**
   class D implements A { public void m1(int x) { } }
 **//this doen't** 
 abstract class G implements A { public void m1(int x) { } }

I have a doubt that why abstract class is able to override and class D can't

If I see the second case

 class X1
{
 public void f2(){}
 }

class X2 extends X1
 {**//No error**
  public void f2(int x){}
}

why public void m1() is not getting overidden in class D whereas same type of method f2() is getting overriden in class X2 In both cases we are overidding but why in interface case class D cant and in second case class X2 can override.

Upvotes: 0

Views: 655

Answers (4)

Jon Skeet
Jon Skeet

Reputation: 1504122

When there are two methods with the same name but different parameter types (or counts), that's overloading, not overriding.

  • D doesn't implement A itself because it doesn't provide an implementation of m1() - no parameters. It tries to provide a method m1(int), but that doesn't help to implement the interface - so it won't compile (as it's not an abstract class). It could provide both methods of course.
  • X1 provides a method f2(), and X2 extends X1 and adds a new overload f2(int) - but it doesn't override the method provided by X1.

In particular, if you write:

X2 x2 = new X2();
x2.f2(10); // Calls X2.f2(int)
x2.f2(); // Calls X1.f2()

Using the @Override annotation makes all of this clearer:

class X1
{
     public void f2(){}
}

class X2 extends X1
{
     @Override public void f2(int x){}
}

This now gives an error:

error: method does not override or implement a method from a super type
     @Override public void f2(int x){}
     ^
1 error

Upvotes: 3

Jim Garrison
Jim Garrison

Reputation: 86774

Neither D nor G override A.m1() -- they "overload" it. Therefore D is missing the implementation of A.m1(). However, G is still abstract and so does not need to implement A.m1().

In the case of X2 you have again overloaded (not overridden) f2. But both of those are concrete methods, so no error occurs.

Upvotes: 0

13Tazer31
13Tazer31

Reputation: 181

In class D you're not actually overriding the method. Since it's an abstract method you're supposed to implement the method in class D. What you're doing however is implementing a new new method, 'void m1(int x)', rather than implementing 'void m1()'. You're overloading the method rather than implementing / overriding it.

The reason you don't get an error when using an abstract class is because you're not obligated to implement all methods from the interface it inherits from. You will however have to implement 'void m1()' in G's subclass, if they're not abstract.

In essence : Change the method signature in interface A to : public void m1(int x); and it'll work.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258698

Bot your classes, D and G, remain abstract, because you're not overriding A.mi1().

You get an error for D because you haven't declared it abstract. Either make it abstract or override A.mi1().

In your second snippet - you're not overriding, but overloading. Those two are completely different functions. Don't be fooled by the fact that they have the same name. The function name isn't the only thing that goes into overload resolution, so do parameters.

Upvotes: 0

Related Questions