• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overriding an abstract method

 
Ranch Hand
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest,

Ok, thanks ... I've got it now. I was just trying to understand how (or why) this works.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic