0

I am trying to pass OCP 8 and I face this code which can override a class method on the fly as it treats it as abstract or interface

package xxx.xxx.ClassC;

 public class ClassC{

  public void print(){`enter code here`

        System.out.println("ClassC method implementation");
     }
   }

 package yyy.yyy.ClassB;

 public class ClassB{

   public static void main(String[] args){

       ClassC ref=new ClassC(){

       public void print(){

          System.out.println("ClassB method implementation");
       }
      };

   ref.print();

  }

}

Result Will be "ClassB method implementation"

Zero Code
  • 19
  • 1
  • 3
  • This is an anonymous class [see documentation](https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html) – Flown Jul 24 '20 at 06:22
  • it's a concrete class but you can override it's method her. – Zero Code Jul 26 '20 at 17:51
  • 1
    Yes. And it’s still an anonymous subclass. – Holger Jul 27 '20 at 15:32
  • Does this answer your question? [Java override method from another class without inheritance](https://stackoverflow.com/questions/16997747/java-override-method-from-another-class-without-inheritance) – Grim Dec 29 '20 at 10:28

0 Answers0