• Post Reply Bookmark Topic Watch Topic
  • New Topic

When do you use abstract methods in an abstract class?  RSS feed

 
Brian Schuetz
Greenhorn
Posts: 25
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • 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?
 
nu nin
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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.

 
Paul Clapham
Sheriff
Posts: 22831
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Paul.

How do you decide when to use abstract methods over defined methods?
 
Paul Clapham
Sheriff
Posts: 22831
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!