6.Overloading, Overriding, Runtime Type and Object Orientation (inner)

1.There is no restriction on the return type. If the parameters are different then the methods are totally different (other than the name) so their return types can be anything.
2.
Note the difference between an Inner class and a Top level nested class. Inner class means a NON STATIC class defined inside a class. Top level nested class means a STATIC class defined inside a class.
For eg.

public class A  // outer class{   static public class B //Top Level Nested class . It can be used in other places: A.B b = new A.B(); There is no outer instance.   {   }   class C //Inner class. It can only be used like this: A.C c = new A().new C(); Outer instance is needed.   {   }}
One can define a class as a static member of any top-level class. Classes which are static class members and classes which are package members are both called top-level classes. They differ from inner classes in a sense that a top-level class can make direct use only of its own instance variables. The ability to nest classes in this way allows any top-level class to provide a package-like organization for a logically related group of secondary top-level classes, all of which share full access to private members.
Now consider the following contents of a file named I1.java ...

public interface I1{    public void mA();    public interface InnerI1    {        int k = 10;        public void innerA();    }}
Here, interface InnerI1 is implicitly STATIC and so is called as TopLevel Nested interface. 'k' is a static (& final) member of this interface.
If you do 'javap' on I1 it prints:
Compiled from I1.java

public interface I1    /* ACC_SUPER bit NOT set */{    public abstract void mA();    public static interface I1. InnerI1    /* ACC_SUPER bit NOT set */    {        public static final int k;        public abstract void innerA();    }}
This interface can be referred to in other places like:

class MyClass implements I1.InnerI1{...}

This is similar to referring a Top Level nested class    
  
3.To override a method in the subclass, the overriding method (ie. the one in subclass) MUST HAVE:
.same name
.same return type
.same no. type and order of parameters
.It may throw only those exceptions which are declared in the throws clause of the superclass's method or exceptions that are subclasses of the declared exceptions. It may also choose NOT to throw any exception.
The names of the parameter types do not matter. For eg. void methodX(int i) is same as void methodX(int k)

4.a non static field value1 is shadowing the static field value1 received from implementing the interface


5.The static variables are class variables(for every instance of a class there is single and same static variable), it makes no sense for a method to have a static variable.

6.private, protected and public can be applied to an Inner class.

7.synchronized keyword can never be applied to a class.It can only be applied to a method or a block

8.Unlike popular belief and unlike mentioned in various books, anonymous class can never be static.  Even if created in a static method.
Anonymous classes are implicitly final.

9The keyword implements is used when a class inherits method prototypes from an interface.
 The  keyword extends is used when an interface inherits from another interface, or a class inherits from another class.

10 
  要特別注意如果父类中有多种构造函数,子類有默認繼承父類的無參數的構造函數,即使子類沒有顯示super()寫出來時,如只有无参,构造,这无须写子构造
    如果父类中没无参数构造函数,子类的构造函数必须调用父类的其它有参数的函数

11 Anonymous inner classes always extend directly from the Object class.

When you create an anonymous class for an interface then it extends from Object for eg.


button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { } } );
But if you make an anonymous class from another class then it extends from that class for eg. consider the following class:

class MyListener implements ActionListener
{
public void actionPerformed(ActionEvent ae) { System.out.println("MyListener class"); }
}
button.addActionListener( new MyListener() {
public void actionPerformed(ActionEvent ae) { System.out.println("Anonymous Listener class"); }
} );

Here the anonymous class actually extends from MyListener class and successfully overrides the actionPerformed() method.

AVI
1.Inner可以放在方法中,if塊,{},可隨時訪問外部變量Out.this.xxx 而且,只可訪問這些中的final局部變量(需要初始化)
2 main中
Out out = new Out();
Out.Inner inner = out.new Inner();
3.Inner類 可以被public,protected,prviate,default,abstract,final,static修飾
4.非static Inner類不能有static 方法
5.Inner類可以 extends 抽象類或其他類,或implements 接口
5.1 通過private Inner implements 接口把 Inner隱藏起來(get接口名()....單例模式)
5.2 當需求中接口中的方法和類中的方法實做不同時.
5.3 Inner可以實現變向的多重繼承

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值