Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract Method

 
Santosh Kumar Nekkanti
Greenhorn
Posts: 8
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
b.callme();
b.callmetoo();
}
}

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
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . 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
Greenhorn
Posts: 8
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Regards,
Santosh N.
 
Gitahi Ng'ang'a
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 8
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Gitahi ,Its clear.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic