The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Help me!

Posted by Kevin on November 29, 2001 at 3:28 PM

I~{!/~}m a tyro in java. I made a test for inheritance and polymorphism. But there is a problem that I can~{!/~}t understand. Who can help me?
The source code is as follows:
The base class is in directory – PKG.

package pkg;

public class Cat
{
protected Cat()
{
System.out.println("Cat construction()");
}
protected void what()
{
System.out.println("This is Cat");
}
protected void finalize() throws Throwable
{
System.out.println("Cat finalize()");
super.finalize();
}
}

The derived classes are in the upward directory of PKG :
import pkg.*;

class BlackCat extends Cat
{
BlackCat()
{
System.out.println("BlackCat construction()");
}
protected void what()
{
System.out.println("This is BlackCat");
}
protected void finalize() throws Throwable
{
System.out.println("BlackCat finalize()");
super.finalize();
}
}
public class BabyBlackCat extends BlackCat
{
BabyBlackCat()
{
System.out.println("BabyBlackCat construction()");
}
protected void finalize() throws Throwable
{
System.out.println("BabyBlackCat finalize()");
super.finalize();
}
protected void what()
{
System.out.println("This is BabyBlackCat");
}
public static void main(String args[])
{
BabyBlackCat babycat = new BabyBlackCat();
//! Cat a = new Cat();
//! Cat b = new BlackCat();
//! Cat c = new BabyBlackCat();
//! a.what();
//! b.what();
//! c.what();
}
}

The problem sentences are flagged by ~{!0~}//!~{!1~}. If you uncomment it, you will get error message – ~{!0~}what() has protected access in pkg.Cat~{!1~}.

Thank you very much.




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us