I am trying to understand the method overriding in java. Understood that private methods cannot be overriden.
public class Superclass
{
private String name;
Superclass(String name) {
this.name=name;
}
private void printName(String name) {
System.out.println("This is a Superclass output"+name);
}
}
public class Subclass extends Superclass
{
Subclass(String name) {
super(name);
}
public void printName(String name) {
System.out.println(name);
}
}
public static void main (String[] args) {
Superclass sb = new Subclass("Superclass");
//System.out.println(sb.getClass().getName());
sb.printName("this is super class variable");
}
I am trying to execute this snippet of code
and the output:"This is a Superclass outputthis is super class variable"
please help me understand to which class does object sb actually refer to.
and the output I see when the printName
is public in the superclass is:
"this is super class variable"
also please help me understand why sb
is pointing to two different class depending upon modifiers private and public.