• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

overriding concrete methods with abstract ones

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
Am new here and feverously studying for the Programmer exam.
I have been reading the Sierra book and noted in there that if you have abstract class A which gives a concrete defintion of method m2, then it is perfectly okay to have another abstract class B which subclasses A and defines the method m2 to then be abstract...

I find this very confusing though the code compiles so it's certainly legal. If B subclasses A that says to me B is more concrete so for it then to specify that the inherited method is more abstract seems strange, what reason would you have to do this?
Many thanks
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am probably guessing your question to be
"whether it is possible for a subclass of an abstract class to be declared abstract too ?"
If that is the question, the answer is yes.As long as you are not going to implement the abstract methods in your sub class, that sub class has to be abstract too
Hope this helps
and good luck
Sri
 
Tyler Durden
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thanks for the reply. It's not quite what I meant, it just seems strange to me to have a concrete method in the superclass which is then declared abstract in the subclass... not sure that it'd be in the exam but I am wondering under what circumstances you'd possibly want to do this?
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry,in all my enthusiasm , I misread the method code in the superclass. I do not know the answer either. We will look for any possible answer from the fellow ranchers.
Thanks
Sri
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wide awake -
You are just talking about only one method. Do you know that an entire class with all methods fully implemented can be declated as abstract (EVEN THOUGH NOT A SINGLE METHOD ON THE CLASS IS ABSTRACT)?
 
Tyler Durden
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am aware of that, just doesn't seem intuitive to me... everything I've learnt about OO says you get more concrete as you go down the inheritance hierarchy, yet this seems to contradict that
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Wide Awake, welcome to JavaRanch.
I agree with you that overriding a concrete method to make it abstract does not feel right -- it seems to imply that the method in the base class should have been abstract to begin with.
However, a possible reason for doing this may be that you don't have the source code for the base class that your class extends, and want to force concrete sub-classes of your class to override a particular method.
There was a similar discussion last month that you may find interesting:
Abstract class
 
Tyler Durden
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!! I wasn't a total newbie and searched through the archives but didn't come across this thread, that and your explanation of when you would need this was really helpful
 
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello wide awake. Welcome to Javaranch.

Please read the Javaranch naming policy and change your display name to a name that at least looks like a real name (if not your own name). You can change it here. Thank you for your co-operation.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!