• Post Reply Bookmark Topic Watch Topic
  • New Topic

overriding an abstract method  RSS feed

 
Keith Flo
Ranch Hand
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the compiler view an exact copy of an abstract method in a child class as an override (see abstract method in Car class below)? This for example compiles and runs :

interface Vehicle {
public abstract void goUpHill(); // Abstract method
}

abstract class Car implements Vehicle {
public abstract void goUpHill(); // Abstract method another copy!
// is this an override? does the compiler view this as an override?
}

public class TestOneMini extends Car {

public static void main(String[] args){
//TestOneMini tm = new TestOneMini();
//tm.goUpHill();

Car tm = new TestOneMini();
tm.goUpHill();

}

public void goUpHill() {
System.out.println("Going up a hill!");
}

}

Thanks in advance!
 
Jesus Angeles
Ranch Hand
Posts: 2070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The one in vehicle is an interface.

The Car implements the vehicle interface.

The Car is not 'overriding' that method. It is 'implementing' it.
 
Keith Flo
Ranch Hand
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No .. I dont think thats true. Car is an abstract class and the goUpHill() method in Car is abstract also! It cannot be an implementation.

kf
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Keith Flo:
No .. I dont think thats true. Car is an abstract class and the goUpHill() method in Car is abstract also! It cannot be an implementation.
kf


This is pretty much an angels-skating-on-the-head-of-a-pin argument, because whether Car includes that abstract method or not, the behavior of the code is identical.

But regardless of the precise technical meanings of the terms involved: yes, the compiler sees that method as overriding the interface method, or as an implementation of that method, or whatever you'd like to call it. The fact that the method is abstract doesn't interfere with the fact that the method signature matches, which is all that matters.
 
Keith Flo
Ranch Hand
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,

Ok, thanks ... I've got it now. I was just trying to understand how (or why) this works.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!