Runtime Polymorphism OOPS Concept

In accordance with the concept of oops. In the case of drag and drop, the reference variable of the parent class cannot execute the private method of the child class, but when I run the following program -

interface My { } class my1 implements My { public String toString() { return "hello"; } public static void main(String... s) { My m=new my1(); System.out.println(m.toString()); } } 

it succeeds and prints "hello." Can someone explain how this happens ... ??

in order to be more clear, the following program gives a compile-time error

 interface My { } class my1 implements My { public String toString1() { return "hello"; } public static void main(String... s) { My m=new my1(); System.out.println(m.toString1()); } } 
+5
source share

Source: https://habr.com/ru/post/1207635/


All Articles