Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Abstract Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract Method" Watch "Abstract Method" New topic

Abstract Method

Santosh Kumar Nekkanti

Joined: Feb 17, 2011
Posts: 8

Hai all,

I am new to java. What is the use of "Abstract Methods", Since we are defining that method in the subclasses,What is the meaning in making it abstract and keeping it in the abstract superclass?
for example:

abstract class A {
abstract void callme(); //line2}

class B extends A {
void callme() {
System.out.println("B's implementation of callme."); // since anyhow we are defining the method here what is the purpose of line2}

class AbstractDemo {
public static void main(String args[]) {
B b = new B();

Please help me in this.I know that even a small thing in java is written with some specific purpose. I am unable to understand the purpose of that abstract class.

Thanks in advance,
Santosh Kumar N.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

Welcome to the Ranch!

Because the method is declared in class A, you can call that method on variables with A as the reference type. InputStream is a good example. The read() method is abstract. That means I can create a variable of type InputStream, assign to it an instance of any sub class (e.g. FileInputStream, ByteArrayInputStream, etc) and still call that method:
Imagine that the read() method was not available in InputStream. The above example wouldn't be able to switch from a FileInputStream to a ByteArrayInputStream without having to change the type of variable "is".

How To Ask Questions How To Answer Questions
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46356
. . . and by writing an abstract method in the superclass, you ask the compiler to ensure it is available in all objects of that type.
Santosh Kumar Nekkanti

Joined: Feb 17, 2011
Posts: 8

Thanks for Rob Spoor & Campbell Ritchie for your immediate response. The answer you gave is too much high level for me since i just started java now,but anyhow i understood.

Santosh N.
Gitahi Ng'ang'a

Joined: Oct 30, 2009
Posts: 8
You use an Abstract class when you want to define a set of common (high level) behavior for a group of things. Think about a computer game in which a player walks though a forest full of wild animals. As he walks through the forest, he may encounter many different kinds of animals. The animals may attack him. Thus we want all animals to be able to attack the player. However, while a snake may attack by biting the player, an elephant may attack by trampling on him. This program can be expressed as follows.

Notice that in the beAttacked() method, we need not specify the specific kind of animal doing the attacking. Our abstract method attack() already guarantees that every animal can attack. What attack means is left to the specific animal to implement. Does that make it clearer?
Santosh Kumar Nekkanti

Joined: Feb 17, 2011
Posts: 8

Thanks Gitahi ,Its clear.
I agree. Here's the link:
subject: Abstract Method
It's not a secret anymore!