• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt about applet

 
felix thomas
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i have some doubts abot apllets and java
in the applet
there is a method called paint(Graphic g)
which takes a abstract class Graphics as an agrument.
later if u want to write nything ont the applet u use g.drawstring() method
how can an abtract class be instantiated
where in java it is known that an abstract class cannot be intialized.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The browser viewing the Applet is responsible for instantiating an object of a type that is the appropriate descendant of the abstract Graphics class. The argument to the paint method is a reference of type Graphics that refers to an object of some type that extends Graphics. Remember, something that extends Graphics is a Graphics type thing.
You cannot directly invoke a constructor of an abstract class with new MyAbstractClass(). When instantiating an object of some type that extends an abstract class, the constructor of the subclass does (and must) invoke a constructor from the parent class. Often, the invocation of the parent's constructor is done implicitly (i.e. the programmer didn't type it in, so the compiler included the instructions to invoke the parent's no-argument constructor) and so, you may not have noticed this before. From section 8.1.1.1 of The Java Language Specification:
A compile-time error occurs if an attempt is made to create an instance of an abstract class using a class instance creation expression.
...
A subclass of an abstract class that is not itself abstract may be instantiated, resulting in the execution of a constructor for the abstract class...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic