diff --git a/divyansh.java b/divyansh.java new file mode 100644 index 0000000..0e4e3ab --- /dev/null +++ b/divyansh.java @@ -0,0 +1,39 @@ +// A Simple Java program to demonstrate +// method overriding in java + +// Base Class +class Parent { + void show() + { + System.out.println("Parent's show()"); + } +} + +// Inherited class +class Child extends Parent { + // This method overrides show() of Parent + @Override + void show() + { + System.out.println("Child's show()"); + } +} + +// Driver class +class Main { + public static void main(String[] args) + { + // If a Parent type reference refers + // to a Parent object, then Parent's + // show is called + Parent obj1 = new Parent(); + obj1.show(); + + // If a Parent type reference refers + // to a Child object Child's show() + // is called. This is called RUN TIME + // POLYMORPHISM. + Parent obj2 = new Child(); + obj2.show(); + } +}