-5

I have an abstract superclass. Some of the methods have implementation in the abstract class. In my subclass would i need to override the implemented method. The method in the subclass does exactly the same as superclass.

public abstract class vehicle {
   public int getNumber() {
      return 5;
   }
} 

public class car {
   public int getNumber() {
      return super.getNumber();
   }
} 

So my question is, do i need to override the method and include a super call? Or can i just leave it in the super class and not implement it at all in the subclass?

Dolda2000
  • 25,216
  • 4
  • 51
  • 92
Waz
  • 161
  • 1
  • 2
  • 10
  • 2
    No, but you do need to close your braces :) – psmears Mar 13 '15 at 21:06
  • 1
    Did you try it? Or read some basic documentation about abstract classes? – Matt Ball Mar 13 '15 at 21:07
  • 6
    Why not just test your code and see for yourself? – Pshemo Mar 13 '15 at 21:07
  • Why all the downvotes? The asker may not have a firm understanding of Java, but I don't see the question itself being all that bad. – Dolda2000 Mar 13 '15 at 21:13
  • 1
    @Dolda2000 I suspect that these down-votes are not effect of posting bad question, but result of not showing research effort. IMO before asking any question first thing every programmer (and not only programmer) should do, is searching for answer by himself, like by writing simple test case similar to one [posted by @CodeWhisperer](http://stackoverflow.com/a/29041948/1393766) which gives answer instantly. Even if OP doesn't have runtime installed there are lot of online-IDE like http://ideone.com/. – Pshemo Mar 13 '15 at 21:28

2 Answers2

2

You can leave it in the superclass and not implement it in the subclass.

calling

car BMW = new car();

BMW.getNumber();

should grab the superclass method.

psmears
  • 26,070
  • 4
  • 40
  • 48
Code Whisperer
  • 1,041
  • 8
  • 16
2

No, you do not. You can just leave it in the super (parent) class and not implement it at all.

The Reason

Basically the idea behind an abstract class (very briefly) is that you can write down common implementations that all the concrete classes derived from the abstract class once, and then not have to override or rewrite them again.

An example like in your case would be if all the vehicles have a number, it is a common property, then there isn't a point in writing the same getNumber() method for each class that derives from that class. The abstract vehicles class is like a 'framework' of sorts, and the concrete classes (car, bus, van) that derive from the vehicle class is the actual class that you instantiate. In this class you can have more specialized variables and methods, (is the car a 2 wheel or 4 wheel? A truck probably doesn't need that method)

Related readings from this SO question: Purpose of Abstract Classes

In short the beauty of abstract classes is that you don't have to be concrete (lol) about every single detail, yet you can have one method for across all common classes, and override them if they are special cases. This helps in maintaining code too (A bug in a common method shared across all classes only requires change in the abstract class and not all of the concrete classes)

Additional Info

Just as additional info, if you did use an interface and NOT an abstract class, then you would HAVE to override each method there, as an interface does not contain the implementation, only the design. This link helped explain things to me last time.

You might also want to read up on Polymorphism and Inheritance, important concepts in all Object oriented Programming (OOP) languages. :)

Note: Also, since I can't comment on your question (not enough rep) yet but have been lurking on SO long enough, don't be discouraged by the downvotes, many here assume you have the right keywords too search for on StackOverflow to get the answer you want but this isn't always the case. Good luck!

Community
  • 1
  • 1
matrixanomaly
  • 6,627
  • 2
  • 35
  • 58