interface in java_java-interface

/*Method. Overriding vs. Overloading.

* If a "subclass" has a method with the exact same signature

* as int the "superclass", we say the subclass overrides the

*the method.

1.Animal's subclass Pig overrides the makeNoise() method.

2.Methods with the same name but different signatures are overload.

**/

/*If a subclass has a method with the exact same signature

* as in the superclass, we say the subclass overrides the method.

*1. Even if you don't write @Override, subclass still overrides the

method.

@Override is just an optional reminder that you are overriding.

*

*Why use @Override ?

1. Main reason: Protects against typos.

1.1 If you say @Override, but it the method isn't

actually overriding anything, you'll get a compiler error.

1.2 public void addLats(Item x).

2. Reminds programmer that method definition came from somewhere

higher up in the inheritance hierarchy.*/

/*Interface Inheriance.

* Specify the capabilities of a subclass using the implements

* keyword is known as interface inheritance.

* 1.1 Interface: The list of all method signatures.

* 1.2 Inheritance: The subclass "inherits" the interface

* from a superclass.

* 1.3 Specifies what the subclass can do, but not how.

* 1.4 Subclasses must override all of the these methods.!

* will fail to compiler otherwise.*/

/*Two seemingly contradictory facts:

* #1. When you set x = y or pass a parameter,

* you're just copying the bits.

* #2. A memory box can only hold 64 bit addresses for the appropriate

* type.

* String x can never hold the 64 bit address of a Dog.

*

* Copying the Bits.

* Answer: If X is a superclass of Y, then memory boxes

* for X may contain Y,

* 1. An AList is-a List.

* 2. Therefore List variable can hold ALList addresses.

* /

/*Implementation Inheritance

* Interface inheritance:

* 1.1 Subclass inherits signature, but NOT implementaion.

*

* For better or worse, Java also allows implementaion inheritance.

* --Subclasses can inherit signatures AND implementation.

*

*Use the default keyword to specify a method that subclasses should

inherit from an interface.

1.1 Example: Let's add a default print() method to List61B.java.

**/

public interfaceAnimal {public voidmakeNoise();

}public classPig implements Animal {public void makeNoise() { //override.

System.out.print("oink");

}

}public classDog implements Animal {public void makeNoise(Dog x) { //overload.//....

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值