java trick

原创 2007年10月05日 15:10:00

// 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;

相关文章推荐

NIO trick and trap .pdf

  • 2017年06月20日 14:51
  • 2.05MB
  • 下载

arp_trick.tar.gz

  • 2014年04月04日 18:19
  • 30KB
  • 下载

python 爬虫及multiprocessing包——多线程爬取并解析百度贴吧某贴小trick

preface:看极客学院关于xpath的视频时,偶然看到可以用multiprocessing进行多线程爬取网页,只有小段代码,故先贴出来。mark下。 coding: #!/usr/bin/env...

list_sticky_scroll_trick

  • 2016年01月02日 10:58
  • 78KB
  • 下载

NIO trick and trap NIO网络

  • 2011年07月08日 21:33
  • 3.04MB
  • 下载

稳扎稳打Silverlight(41) - 3.0Tip/Trick之GPU 加速, Out-Of-Browser, 应用程序库缓存, 合并 ResourceDictionary, 应用程序扩展服务,

[索引页] [源码下载] 稳扎稳打Silverlight(41) - 3.0Tip/Trick之GPU 加速, Out-Of-Browser, 应用程序库缓存, 合并 Resource...

poj 3207 Ikki's Story IV - Panda's Trick 2-sat

题意: 在一个圆上有n个点,m条边将他们相连,问是否有一种连发使没有两条线相交。 分析: 每条线要么在圆内,要么在圆外,2-sat问题。 代码: //poj 3207 //sep9 #inc...
  • sepNINE
  • sepNINE
  • 2014年12月08日 13:58
  • 494

【POJ 3207】Ikki's Story IV - Panda's Trick

【POJ 3207】Ikki's Story IV - Panda's Trick 2-SAT判断可行性模板题~

Ikki's Story IV - Panda's Trick poj3207 tarjan+2-SAT

题意/Description:     liympanda, one of Ikki’s friend, likes playing games with Ikki. Today after mine...

poj 3207 Ikki's Story IV - Panda's Trick#2-sat

/** 裸地2-sat问题。关键在于找到其中的矛盾关系。 一个圈上n个点,要连m条边,要求边只能连在圈内或圈外,且边与边不能相交。 把每条边拆成两条,圈内一条,圈外一条, 则两条边的矛盾关...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:java trick
举报原因:
原因补充:

(最多只允许输入30个字)