• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Abstract Method

 
Greenhorn
Posts: 8
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Marshal
Posts: 76845
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Gitahi ,Its clear.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic