• Post Reply Bookmark Topic Watch Topic
  • New Topic

Abstract Methods and Classes  RSS feed

 
Steven Alvarez
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please explain what a abstract method and class is? Please keep it simple. Also why does a abstract method contain no body? Thanks for any help!!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract classes and methods allow a class designer to implement part of a class but force some other class to implement the rest.

If a class is marked abstract you can't create an instance. So "new AbstractButton()" won't compile. We have to have another class that extends the abstract class and fills in some missing bits. So "new JButton()" is fine.

Abstract methods name the parts that other classes are required to implement. Since we're making someone else implement them, they can be empty in the abstract class.

Here's a tiny example - not compiled or checked for complete accuracy but hopefully instructive ...

Now when you make a ConcreteThing that extends AbstractThing you are required to write doSomethingInteresting, but you don't have to write setUp and tearDown. In fact you can't even see them. People who use ConcreteThing just call execute(). They don't know anything about the complex setup, do the work, teardown sequence. Neat, huh?
[ November 23, 2006: Message edited by: Stan James ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An abstract method is simply a method declaration without implementation (without a body). The implementation is deferred to a subclass, where the abstract method is overridden and a body is provided. This way, different subclasses can implement the method differently. But they can still be upcast to the abstract type, where the method is available. Polymorphism invokes the correct method implementation.

In other words, declaring an abstract method is like saying, "I know I need a method by this name, but exactly how it works will depend on the subclass."

An abstract class is any class that might contain an abstract method. (If a class contains an abstract method, then the class itself must be declared abstract. On the other hand, if a class is declared abstract, it is not required to contain an abstract method.)

Here's an example.
 
Ravindranath Chowdary
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marc,
Your explanation is good. Now, i need why i cant use static along with abstract.
ex:
static void makeNoise()

Please can you give me a clear idea why i should not use both.

Thanks,
Ravindranath.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it were legal to combine static and abstract modifiers, what would you expect it to mean?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ravindranath Chowdary:
Now, i need why i cant use static along with abstract.
ex:
static void makeNoise()


Do you want to know why you can't declare a single method both static and abstract? or are you asking if you can have a static method in an abstract class?
 
Ravindranath Chowdary
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know why can't we declare a method with both static and abstract?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!