• 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

When do you use abstract methods in an abstract class?

 
Greenhorn
Posts: 25
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just have a simple question. When creating an abstract class, do you always make your methods abstract? Or can you declare them with a body (and not abstract)? How do you know when to make a method abstract?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When you create an abstract class if methods are not defined you have to declare them as abstract and overwritte them in every class is inherit (Just the not definded methods).
Defined methods can have logic without being abstracts

ex.

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In an abstract class you don't need to make all your methods abstract. In fact you don't need to make any of them abstract. However in most cases, the designer will make some of the methods abstract and some of them not abstract (i.e. with code).
 
Brian Schuetz
Greenhorn
Posts: 25
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul.

How do you decide when to use abstract methods over defined methods?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The purpose of an abstract method is to provide a name (and signature) of a method which must be implemented by every concrete subclass of the abstract class. So if you are designing a type which must have a particular method to do its job, but you expect that subclasses of that type will have a variety of ways to implement that method, then that's when you might use an abstract method.

That may sound unclear to you, but if you're a beginner then you may never have designed a class in your life. Typically you're told to write a particular piece of code, and learning to design classes comes along later. And if you're not especially familiar with inheritance of classes, which also doesn't get taught to beginners right away, then you might well not understand why that's a useful design feature.
 
Brian Schuetz
Greenhorn
Posts: 25
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, that answered my question. I understood what you were saying. I was thinking something along what you were saying but just needed it explained for it to click. Thanks again, Paul.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic