GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract class" Watch "Abstract class" New topic
Author

Abstract class

saravanan ks
Greenhorn

Joined: Jul 10, 2003
Posts: 9
import java.util.*;

abstract class Instrument4
{
int i; // storage allocated for each

public String what()
{
return "Instrument4";
}
public abstract void play();
public abstract void adjust();
}

class Wind4 extends Instrument4
{
public void play()
{
System.out.println("Wind4.play()");
}
public String what()
{
return "Wind4";
}
public void adjust() {}
}
class Percussion4 extends Instrument4
{
public void play()
{
System.out.println("Percussion4.play()");
}
public String what()
{
return "Percussion4";
}
public void adjust()
{}
}

class Stringed4 extends Instrument4
{
public void play()
{
System.out.println("Stringed4.play()");
}
public String what()
{
return "Stringed4";
}
public void adjust()
{}
}

public class Music4
{
// Doesn't care about type, so new types
// added to the system still work right:

static void tune(Instrument4 i)
{
System.out.println("I'm into tune method()\n");
i.play();
}
static void tuneAll(Instrument4[] e)
{
System.out.println("I'm into tuneall method()\n");
for(int i = 0; i < e.length; i++)
tune(e[i]);
}
public static void main(String[] args)
{
Instrument4[] orchestra = new Instrument4[5];
int i = 0;

// Upcasting during addition to the array:

orchestra[i++] = new Wind4();
orchestra[i++] = new Percussion4();
orchestra[i++] = new Stringed4();
tuneAll(orchestra);
}
} ///:


Above programs I seen in a book while I'm working in java, since I new to java I'm not able to understand few things, here are

1. First we can't instanciate abstract class they have done like this

Instrument4[] orchestra = new Instrument4[5];

2. orchestra[i++] = new Wind4(); what this line specifies. How to understand this line.what they are doing on line please anyone clarify would be great help.

Thanks in advance

- saravanan
Barry Higgins
Ranch Hand

Joined: Jun 05, 2003
Posts: 89
Righto!
First of all you have an abstract class Instrument. What this says is I am a class that cannot be instantiated and anyone who inherits from me must use the methods that I have declared in me.

What you have delared here is only a declaration of an array of Instruments of size 5.
You have not instantiated the object itself (yet!).

Instrument4[] orchestra = new Instrument4[5];

Next you have a lot of implementations of your abstract class.
Wind4, Percussion4 and Stringed4 these inherit from Instrument are not abstract themselves because they declare all the methods in Instrument so it's fair enough to instantiate these classes.

orchestra[i++] = new Wind4()

if you look at what is being said here it's the same as

Instrument4 orchestra = new Wind4();

without the array stuff to worry about which is really just decaring a new Wind4 and assigning it to orchestra. orchestra is of type Intrument4 so we can assign a Wind4 to it because Wind4 subclasses (inherits from) Instrument.

Now the orchestra[i++] bit is just saying because orchestra is actually an array of Instrument4s that you are assigning Wind4 to that index of the array.
Phew! Sorry for the long winded (no pun intended) answer but hope that helps
Barry
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724

What this says is I am a class that cannot be instantiated and anyone who inherits from me must use the methods that I have declared in me.


Exactly you have to implement only abstract methods declared in abstract class.
[ November 04, 2004: Message edited by: David Ulicny ]

SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817


First of all you have an abstract class Instrument. What this says is I am a class that cannot be instantiated and anyone who inherits from me must use the methods that I have declared in me.


Just to clarify, it is permitted to override the declared methods -- you don't have to use only the ones that are declared.

You do, however, have to implement the methods that are declared abstract. (Or your subclass has to be declared abstract....)


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
saravanan ks
Greenhorn

Joined: Jul 10, 2003
Posts: 9
Thanks a lot friends, I have got cleared with your answers.

Regards
Saravanan.
 
GeeCON Prague 2014
 
subject: Abstract class