Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Object-oriented language basics, Part 4

Inheritance: Build objects in layers

  • Print
  • Feedback

Page 5 of 10

class Point
{
   void draw ()
   {
      System.out.println ("I am a point.");
   }
}
class Circle extends Point
{
   void draw ()
   {
      System.out.println ("I am a circle.");
   }
}


According to the code fragment above, if you execute Circle c = new Circle (); c.draw ();, then I am a circle. will be your output. However, if Circle does not override draw() with its own method, you'll see I am a point. instead.

Suppose you have a subclass with a field and/or method that is identical to a superclass field and/or method and you want to access the superclass field or method from the subclass method. How do you accomplish that task? You'll find the answer in the following code fragment:

  • Print
  • Feedback

Resources