aspose file tools*
The moose likes Beginning Java and the fly likes Can be Instantiate Abstract Class ?? If no , look at the code. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can be Instantiate Abstract Class ?? If no , look at the code." Watch "Can be Instantiate Abstract Class ?? If no , look at the code." New topic
Author

Can be Instantiate Abstract Class ?? If no , look at the code.

Jaydeep Singh
Ranch Hand

Joined: Oct 02, 2000
Posts: 43
Hi Friends,
This is the solved exmaple from Thinking In JAVA, CHAPTER 07/
As we know abstract class can not be instantiated, but in this code , I have seen it. Even it compiles fine and working also.
Pl. can anybody explain it.

Thanks In Advance.
Jaydeep
//: c07:music4:Music4.java
// Abstract classes and methods.
import java.util.*;
abstract class Instrument {
int i; // storage allocated for each
public abstract void play();
public String what() {
return "Instrument";
}
public abstract void adjust();
}
class Wind extends Instrument {
public void play() {
System.out.println("Wind.play()");
}
public String what() { return "Wind"; }
public void adjust() {}
}
class Percussion extends Instrument {
public void play() {
System.out.println("Percussion.play()");
}
public String what() { return "Percussion"; }
public void adjust() {}
}
class Stringed extends Instrument {
public void play() {
System.out.println("Stringed.play()");
}
public String what() { return "Stringed"; }
public void adjust() {}
}
class Brass extends Wind {
public void play() {
System.out.println("Brass.play()");
}
public void adjust() {
System.out.println("Brass.adjust()");
}
}
class Woodwind extends Wind {
public void play() {
System.out.println("Woodwind.play()");
}
public String what() { return "Woodwind"; }
}
public class Music4 {
// Doesn't care about type, so new types
// added to the system still work right:
static void tune(Instrument i) {
// ...
i.play();
}
static void tuneAll(Instrument[] e) {
for(int i = 0; i < e.length; i++)
tune(e[i]);
}
public static void main(String[] args) {
Instrument[] orchestra = new Instrument[5];
int i = 0;
// Upcasting during addition to the array:
orchestra[i++] = new Wind();
orchestra[i++] = new Percussion();
orchestra[i++] = new Stringed();
orchestra[i++] = new Brass();
orchestra[i++] = new Woodwind();
tuneAll(orchestra);
}
} ///:~
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
The abstract class isn't being instanciated, the subclasses are. You can use a super class refernce to refer to any subclass type, including an abstract super class.
orchestra[i++] = new Wind();
orchestra[i++] = new Percussion();
orchestra[i++] = new Stringed();
orchestra[i++] = new Brass();
orchestra[i++] = new Woodwind();
This is one of the advantages of polymorphism. As you know an Array must contain the same type for all elements. Since all the suclasses Wind, Percussion, Stringed, Brass and Woodwind are subclasses of Instrument, an Instrument refernece can be used to refer to the objects. If this doen't clear things up, read How my Dog learned Polymorphism
Hope This Helps
[This message has been edited by Carl Trusiak (edited November 21, 2000).]


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
No, you can not create an instance of an abstract class. In the example that you posted, you have created instances of various subclasses of an abstract class, and you have created an array of objects of type instrument (an abstract class). You store subclasses of instrument in the array though because, even though the array is of Instrument objects, you can never have an instrument object, only subclasses (which will work anywhere that the superclass is allowed).
When you think about this example, it makes sense. You can identify a Tuba or a violin. You can recognize a flute or a trombone. But what is meant by "instrument"? Instrument really defines certain behaviors that subclasses of instrument should have. In this example, before you can call an object an instrument, you must be able to play it. Instrument its self is too general to refer to a real object.
I hope that helps.
Jaydeep Singh
Ranch Hand

Joined: Oct 02, 2000
Posts: 43
I just overlooked. I'm sorry. about it. similar, which is not possible.
Instrument[] orchestra = new Instrument[5];

Instrument() orchestra = new Instrument();
Thanks Carl & Bodie,
I understand clearly, I got confused because of the little similarity in above lines.
Kind Regards.
Jaydeep

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can be Instantiate Abstract Class ?? If no , look at the code.