From 20974930631717186e3d018dcfb14cc9e4b9b135 Mon Sep 17 00:00:00 2001 From: divyansh739 <115534606+divyansh739@users.noreply.github.com> Date: Sat, 22 Oct 2022 19:21:12 +0530 Subject: [PATCH] Create divyansh.java --- divyansh.java | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 divyansh.java 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(); + } +}