Skip to content

Instantly share code, notes, and snippets.

@kevin-lee
Last active March 24, 2017 03:07
Show Gist options
  • Save kevin-lee/5840912 to your computer and use it in GitHub Desktop.
Save kevin-lee/5840912 to your computer and use it in GitHub Desktop.
Example code of overriding and hiding methods. Check out my comments on http://goo.gl/wZL96
class SuperClass
{
public static void runStatic(String code)
{
System.out.println(code + ": I am the runStatic method in SuperClass.");
}
public static void runStatic2(String code)
{
System.out.println(code + ": I am the runStatic2 method in SuperClass.");
}
public void run(String code)
{
System.out.println(code + ": I am the run method in SuperClass.");
}
}
class SubClass extends SuperClass
{
public static void runStatic(String code)
{
System.out.println(code + ": I am the runStatic method in SubClass.");
}
@Override
public void run(String code)
{
System.out.println(code + ": I am the run method in SubClass.");
}
}
public class OverridingAndHidingTest
{
public static void main(String[] args)
{
SuperClass super1 = new SuperClass();
System.out.println("SuperClass super1 = new SuperClass()");
super1.run("super1.run()");
// "I am the run method in SuperClass."
super1.runStatic("super1.runStatic()");
// "I am the runStatic method in SuperClass."
// These are obvious.
System.out.println();
SuperClass sub1 = new SubClass();
System.out.println("SuperClass sub1 = new SubClass()");
sub1.run("sub1.run()");
// "I am the run method in SubClass."
// As you know, the run method in the SubClass object in overridden.
sub1.runStatic("sub1.runStatic()");
// "I am the runStatic method in SuperClass."
// The type of sub1 variable is SuperClass so it calls the method in the
// SuperClass not in the SubClass which means hiding works only when it's
// called using ClassName.staticMethod().
System.out.println();
SubClass sub2 = new SubClass();
System.out.println("SubClass sub2 = new SubClass()");
sub2.run("sub2.run()");
// "I am the run method in SubClass."
// sub2 has an instance of SubClass so it executes the one in the SubClass.
sub2.runStatic("sub2.runStatic()");
// "I am the runStatic method in SubClass."
// The type of the variable, sub2 is SubClass so calling runStatic()
// executes the class method, runStatic, in the SubClass.
System.out.println();
SuperClass super2 = sub2;
System.out.println("SuperClass super2 = sub2");
// Now reassign an instance of SubClass (more precisely the reference to an
// instance of SubClass) to the variable super2 the type of which is
// SuperClass.
super2.run("super2.run()");
// "I am the run method in SubClass."
// Although the type of super2 variable is SuperClass, it contains a
// reference to an instance of SubClass so it executes the overridden run()
// method in the SubClass.
super2.runStatic("super2.runStatic()");
// "I am the runStatic method in SuperClass."
// The type of super2 is SuperClass and the static methods belong to the
// class but not instance so it doesn't care about the instance and calls
// the runStatic method in the class that is SuperClass.
System.out.println();
SubClass sub3 = (SubClass) super2;
System.out.println("SubClass sub3 = (SubClass) super2");
sub3.run("sub3.run()");
// "I am the run method in SubClass."
sub3.runStatic("sub3.runStatic()");
// "I am the runStatic method in SubClass."
// Reassigning it to SubClass type variable makes it use the runStatic
// method in the SubClass.
System.out.println();
SubClass.runStatic("SubClass.runStatic()");
// "I am the runStatic method in SubClass."
// SubClass.runStatic() hides SuperClass.runStatic()
SubClass.runStatic2("SubClass.runStatic2()");
// "I am the runStatic2 method in SuperClass."
System.out.println();
SuperClass.runStatic("SuperClass.runStatic()");
// "I am the runStatic method in SuperClass."
SuperClass.runStatic2("SuperClass.runStatic2()");
// "I am the runStatic2 method in SuperClass."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment