• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

doubt about applet

 
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.
 
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...

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!