K
kk_oop
This seems like odd Java behavior. Can someone explain the logic
behind it.
Condider these classes:
public class Base
{
public void printText()
{
printName();
}
protected void printName()
{
System.out.println("I am Base!");
}
}
public class Derived extends Base
{
protected void printName()
{
System.out.println("I am Derived!");
}
}
//main class
public class FirstClass
{
public static void main(String[] args)
{
Derived der = new Derived();
Base base = new Base();
System.out.println("Should be base");
base.printText();
System.out.println("\nShould be derived");
der.printText();
}
}
As I would expect, der.printText prints "I am Derived!" However, if I
make printName private in the Base class, der.printText prints "I am
Base!" Why is that? Clearly, Derived's printName is still in scope to
Base's printText. What's going on here?
Thanks for any info,
Ken
behind it.
Condider these classes:
public class Base
{
public void printText()
{
printName();
}
protected void printName()
{
System.out.println("I am Base!");
}
}
public class Derived extends Base
{
protected void printName()
{
System.out.println("I am Derived!");
}
}
//main class
public class FirstClass
{
public static void main(String[] args)
{
Derived der = new Derived();
Base base = new Base();
System.out.println("Should be base");
base.printText();
System.out.println("\nShould be derived");
der.printText();
}
}
As I would expect, der.printText prints "I am Derived!" However, if I
make printName private in the Base class, der.printText prints "I am
Base!" Why is that? Clearly, Derived's printName is still in scope to
Base's printText. What's going on here?
Thanks for any info,
Ken