File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract class

 
saravanan ks
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 724
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Joel McNary
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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....)
 
saravanan ks
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot friends, I have got cleared with your answers.

Regards
Saravanan.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic