Inheritance

Java Inheritance

Method Overloading

  • Java allows multiple methods with same name, but different parameters

Interfaces & Classes

  • Interface is a specification of what a List is able to do, not how to do it
public interface List61B<Item> {
   public void addFirst(Item x);
   public void addLast(Item y);
   public Item getFirst();
   public Item getLast();
   public Item removeLast();
   public Item get(int i);
   public void insert(Item x, int position);
   public int  size();
}
  • use the implements keyword to tell Java compiler that SLList and AList are hyponyms of List61B
public class AList<Item> implements List61B<Item> {

}

Overloading vs Overriding

Overriding
  • if a subclass has a method with the exact same signature as in the superclass => override
Overloading
  • methods with the same name but different signatures are overloaded
public void makeNoise()
public void makeNoise(Dog x)

public int abs(int a)
public double abs(double a)
  • The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.

  • No. C++ and Java both disallow overloading on a functions’s return type.

Notation
  • adding the @Override notation
    • with this tag, the code won’t compile if it is not actually an overriding method => protection against typos
    • optional
Interface Inheritance
  • specifying the capabilities of a subclass using the implements keyword is known as interface inheritance
  • interface: the list of all method signatures
  • subclass must override all of those methods!
    • otherwise will fail to compile
Implementation Inheritance
  • interface inheritance

    • subclass inherits signatures, but NOT implementations
  • Allow implementation inheritance

    • subclasses can inherits signatures AND implementations
  • use default keyword to specify a method that subclasses should inherit from an interface

if X is a superclass of Y, then an X variable can hold a reference to a Y

Static and Dynamic Type
  • static type (compile-time type): specified at declaration
  • dynamic type (runtime type): specified at instantiation (when using new)
    • equal to the type of the object being pointed to
Dynamic Method Selection for Overriden Methods
  • if Y overrides the method, Y’s method is used instead
public interface Animal {
	default void greet(Animal a) {
		print("hello animal");
	}
	default void sniff(Animal a) {
		print("sniff animal");
	}
	default void praise(Animal a) {
		print("u r cool anima");
	}
}
public class Dog implements Animal {
  @Override
  void sniff(Animal a) {
    print("dog sniff animal"); 
   }
  void praise(Dog a) {
    print("u r cool dog"); 
   }
}
Animal a = new Dog();
Dog d = new Dog();
a.greet(d);  // "hello animal"
a.sniff(d);  // "dog sniff animal"
d.praise(d); // "u r cool dog"
a.praise(d); // “u r cool animal”

praise is OVERLOADED!!! NOT OVERRIDEN!!!

Implementation Inheritance: Extends
  • because of extends, subclass/derived class inherits all members of the parent class:
    • all instance and static variables
    • all methods
    • all nested class
  • except private members
  • constructors are not inherited
    • Java will automatically call super() for you
    • but you have to call super(x) with argument yourself

Encapsulation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值