Archive for August, 2008

Overloading properties in Java

public class TestMain

{

	public static void main(String[] args)

	{

		/* we want "B" with the use of the function from A

		 * (without copying the function) */

		System.out.println(new B().getClassname());

	}

}

abstract public class A

{

	protected String classname = "A";

	String getClassname(){

		return classname;

	}

}
public class B extends A

{

	protected String classname = "B";

}

That doesn’t work.

This will:

public class B extends A

{

	{ classname = "B" ; }

}

You can’t make the property final in A then, though.

No Comments