java trick

// What will happen when you attempt to compile and run the following code?

 

class  Base 
{
    
int i = 99;
    
public void amethod()
    
{
        System.out.println(
"Base.amethod()");
    }

        
    Base()
    
{
        amethod();
    }

}

public   class  Derived  extends  Base
{
    
int i = -1;
        
    
public static void main(String argv[])
    
{
        Base b 
= new Derived();
        System.out.println(b.i);
        b.amethod();
    }


    
public void amethod()
    
{
        System.out.println(
"Derived.amethod()");
    }

}
 

answer:

Derived.amethod()
99
Derived.amethod()
-------------------------------------------------
The reason is that this code creates an instance of the Derived class but assigns it to a reference of a the Base class. In this situation a reference to any of the fields such as i will refer to the value in the Base class, but a call to a method will refer to the method in the class type rather than its reference handle. But note that if the amethod() was not present in the base class, then compilation error would be reported as at compile time. When the compiler sees the statement like b.amethod(), it checks if the method is present in the base class or not. Only at the run time it decides to call the method from the derived class.

 

// Read the code below. Will be the result of attempting to compile and run the code below. 
public   class  AQuestion 
    
public void method(Object o)
        System.out.println(
"Object Verion"); 
    }
 
    
public void method(String s)
        System.out.println(
"String Version"); 
    }

    
public static void main(String args[])
        AQuestion question 
= new AQuestion(); 
        question.method(
null);     
    }

}

//The code compiles cleanly and shows "String Version"

Read the code below. Will be the result of attempting to compile and run the code below.

public   class  AQuestion
    
public void method(StringBuffer sb)
        System.out.println(
"StringBuffer Verion"); 
    }
 
    
public void method(String s)
        System.out.println(
"String Version"); 
    }

    
public static void main(String args[])
        AQuestion question 
= new AQuestion(); 
        question.method(
null);     
    }

}

The code throws an Exception at Runtime.

public interface AQuestion{ 
	public abstract void someMethod() throws Exception; 
} 


A Class implementing this interface should
1. Necessarily be an abstract class.
2. Should have the method public abstract void someMethod();
3. Should have the method public void someMethod() which has to throw an exception which is a subclass of java.lang.Exception.
4. Should have the method public void someMethod() which need not throw an Exception.
[ 4 is right ]

 

public   class  AQuestion
    
private int i = j; 
    
private int j = 10

    
public static void main(String args[])
        System.out.println((
new AQuestion()).i); 
    }

}
1. Compiler error complaining about access restriction of private variables of AQuestion.

2. Compiler error complaining about forward referencing.

3. No error - The output is 0;

4. No error - The output is 10;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值