Skip to content

Instantly share code, notes, and snippets.

@mayanknc
Created July 13, 2017 16:05
Show Gist options
  • Save mayanknc/5fea22262e09cd0cd171b96502491074 to your computer and use it in GitHub Desktop.
Save mayanknc/5fea22262e09cd0cd171b96502491074 to your computer and use it in GitHub Desktop.
package com.mayanknc1;
public class Main {
public static void main(String[] args) {
//Runtime polymorphism can't be achieved by data members. It can be achieved by Methods only
A x = new A();
x.print();
System.out.println(x.v);
A y = new B();
y.print();
System.out.println(y.v);
A z = new C();
z.print();
System.out.println(z.v);
z.print2();
z.print3();
}
}
class A {
String v = "variable of A";
void print() {
System.out.println("print of A");
}
void print2() {
System.out.println("print2 of A");
}
void print3() {
System.out.println("print3 of A");
}
}
class B extends A {
String v = "variable of B";
void print() {
System.out.println("print of B");
}
void print2() {
System.out.println("print2 of B");
}
}
class C extends B {
String v = "variable of C";
void print() {
System.out.println("print of C");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment