EE5805-Java-Lecture3-Inheritance and Polymorphism

Intended Learning Outcomes

  • To derive a subclass from a superclass through inheritance.
  • To invoke the superclass’s constructors and methods using the super
    keyword.
  • To override methods in the subclass.
  • To distinguish differences between overriding and overloading.
  • To understand object casting and explain why explicit down-casting is
    necessary.
  • To understand polymorphism and dynamic binding.
  • To restrict access to data and methods using the protected visibility
    modifier.
  • To declare constants, unmodifiable methods, and nonextendable classes
    using the final modifier.

Inheritance

Definition: one class(A)can inherit all the members(data field and method)from another class(B)
Advantage: Reusable and reduce complexity

Extending a Class

  1. Using the keyword extends
  2. You also can add new fields and methods
  3. Override the methods inherited from the superclass

Method Overriding

Method overriding refers to the process of defining a method in a subclass that has the same name, parameters, and return type as a method in its superclass, thus replacing the implementation of the parent method. Method overriding is a way to achieve polymorphism, which allows a subclass to provide its own implementation and change the behavior of the method.

  1. The access modifier of the method in the subclass cannot be more restrictive than that of the method in the superclass. For example, if the method in the superclass is public, the method in the subclass **y cannot be private or protected.
  2. When using method overriding, the @Override annotation can be used to indicate that the method in the subclass is overriding the method in the superclass.
  3. A subclass cannot weaken the accessibility of a method
    defined in the superclass.(Same with 1)
    Compared with Overloading in lecture1:
    Method overloading is a feature in Java that allows a class to have multiple methods with the same name but different parameters. In method overloading, methods are differentiated by the number, type, and order of their parameters. This means that two or more methods can have the same name, but they must have different parameter lists.

this and super

  1. They are keyword.this is used to refer to a member (method or data field)
  2. super is used to refer to the member of the superclass

A Example

public class Apple extends Fruit {
public static void main(String[] args) {
Apple myApply = new Apple();
}
}
class Fruit {
public Fruit(String name) {
System.out.println("Fruit's constructor is invoked");
}
}

When a child class object is created, its parent class constructor is called first, followed by the child class constructor. If the child class constructor does not explicitly call the parent class constructor, the compiler will implicitly call the default constructor of the parent class. However, in this case, the parent class Fruit does not have a default constructor,only a parameterized constructor, so the compiler will throw an error.

To fix this issue, you can explicitly call the parent class Fruit constructor in the constructor of the child class Apple using the “super” keyword, like this:

public class Apple extends Fruit {
    public Apple(String name) {
        super(name);
        System.out.println("Apple's constructor is invoked");
    }

    public static void main(String[] args) {
        Apple myApple = new Apple("apple");
    }
}

class Fruit {
    public Fruit(String name) {
        System.out.println("Fruit's constructor is invoked");
    }
}

The root type

请添加图片描述

Polymorphism

分为运行时多态和编译时多态,分别对应override和overload

  1. Compile-time polymorphism —— overloading. It occurs when there are multiple methods with the same name in a class, but with different parameters.
  2. Runtime polymorphism —— overriding. It occurs when a subclass provides its own implementation of a method that is already defined in its parent class. The method in the subclass must have the same name, return type, and parameters as the method in the parent class.

instanceof Operator

Use the instanceof operator to test whether an object is an instance of a class.请添加图片描述

The protected Modifier

The protected modifier can be applied on data and methods in a class. A protected data or a protected method in a public class can be accessed by any class in the same package or its subclasses, even if the subclasses are in a
different package.

The final Modifier

Same with the lecture 1,the final can be used to declare a constant.It also can be applied to a class and method
请添加图片描述

Some useful APIS
Scanner
  1. You can get input from console using Scanner object:
Scanner scanner = new Scanner(System.in);
  1. Use these methods next(), nextByte(), nextShort(), nextInt(), nextLong(), nextFloat(), nextDouble(), or nextBoolean() to obtain to a string, byte, short, int, long, float, double, or boolean value. For example,
System.our.print("Enter a double value:);
Scanner scanner = new.Scanner(System.in);
double d = scanner.nextDouble();
Searching Arrays
  1. Java provides several overloaded binarySearch methods for searching a key in an array of int, double, char, short, long, and float in the java.util.Arrays class. For example, the following code searches the keys in an array of numbers and an array of characters.
    请添加图片描述
  2. if an element does not exist, a negative value will be returned.
  3. The array must be pre-sorted in increasing order, otherwise the results are
    undefined.
  4. If the array contains multiple elements with the specified value, there is no
    guarantee which one will be found.
Sorting Arrays

请添加图片描述
The java.util.Arrays.sort() method sorts the elements of the given array in ascending order. The method does not return a new sorted array, instead, it sorts the elements of the original array in place.

This means that the original array is modified and the elements are rearranged to be in ascending order. The sorting is done by comparing the elements of the array, and swapping them if they are in the wrong order. The sorting algorithm used by the Arrays.sort() method is a variant of quicksort.

By sorting the array in place, the Arrays.sort() method is more memory-efficient than creating a new sorted array. This is because creating a new array requires allocating memory for the new array, copying the elements from the original array to the new array, and then sorting the new array. Sorting the original array in place avoids the need for these additional memory allocations and copies.

Note that if you want to keep the original array intact and create a new sorted array, you can use the Arrays.copyOf() method to create a copy of the original array, and then sort the copy using the Arrays.sort() method.

ArrayList

请添加图片描述

  • 19
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值