Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem calling a method in a constructor
- // See below
- // Constructing Sub invokes the constructor of the base class, and this invokes method
- // But Sub has overridden method, and the over-ridden method has been invoked.
- // If we've forgotten that method is involved in the object construction, this might be a disaster
- public class Test {
- public static void main(String[] args) {
- Sub sub=new Sub(); // outputs Sub
- }
- }
- class Base
- {
- Base()
- {
- method();
- }
- void method()
- {
- System.out.println("Base");
- }
- }
- class Sub extends Base
- {
- void method()
- {
- System.out.println("Sub");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment