oop - Inheritance behaviour in java -
can explain , why shows error in eclipse , run without error.i have paste code below.
parent class:
public class parent { /*parent class method*/ public void show() { system.out.println("parent class show called"); } }
child class:
public class child extends parent { /* child class overridden method*/ private void show() { // line show error in eclipse system.out.println("child class show called "); } public static void main(string[] args) { parent p = new child(); p.show(); } }
output is: parent class show called
this happening because eclipse compiler can create class files in presence of compilation errors. please follow below link .
but if open child class created in java decompiler see below code.
public class child extends parent { private void show() { throw new error("unresolved compilation problem: cannot reduce visibility of inherited method parent"); } public static void main(string[] args) { parent p = new child(); p.show(); } }
so eclipse doing ignoring error , creating class file error , when class file present can run code , because @ compile time able find out parent has method show calling method. if change reference parent child class give exception @ runtime.
exception in thread "main" java.lang.error: unresolved compilation problem: cannot reduce visibility of inherited method parent @ com.nucleus.finnone.tbs.child.show(child.java:5) @ com.nucleus.finnone.tbs.child.main(child.java:12)
Comments
Post a Comment