Skip to content

Instantly share code, notes, and snippets.

@sermojohn
Created June 9, 2015 08:17
Show Gist options
  • Save sermojohn/efe1f5177e5393e67212 to your computer and use it in GitHub Desktop.
Save sermojohn/efe1f5177e5393e67212 to your computer and use it in GitHub Desktop.
NestedClassesTest
package ocp;
/** Static methods of hierarchy classes are called on the reference variable type. */
public class NestedClassTest {
/** Nested classes need to be static in order to define static methods (Nested class, not Inner) */
static class A {
public void parentMethod() {
System.out.println("parent method");
}
public static void someMethod() {
System.out.println("A method");
}
}
static class B extends A {
public static void someMethod() {
System.out.println("B method");
}
}
static class C extends B {
public static void someMethod() {
System.out.println("C method");
}
}
public static void main(String args[]) {
C c = new C();
c.someMethod();
c.parentMethod();
B b = c;
b.someMethod();
b.parentMethod();
A a = b;
a.someMethod();
a.parentMethod();
}
}
@sermojohn
Copy link
Author

Output:
C method
parent method
B method
parent method
A method
parent method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment