aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract Method" Watch "Abstract Method" New topic
Author

Abstract Method

Santosh Kumar Nekkanti
Greenhorn

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();
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
. . . 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

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.

Regards,
Santosh N.
Gitahi Ng'ang'a
Greenhorn

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
Greenhorn

Joined: Feb 17, 2011
Posts: 8

Thanks Gitahi ,Its clear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract Method
 
Similar Threads
OOP?
Question on synchronize block
StringBuffer
Dynamic dispatch
String array Vs String var args