• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
} ///:~
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

reply
    Bookmark Topic Watch Topic
  • New Topic