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
- Using the keyword extends
- You also can add new fields and methods
- 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.
- 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.
- 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.
- 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
- They are keyword.this is used to refer to a member (method or data field)
- 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
- Compile-time polymorphism —— overloading. It occurs when there are multiple methods with the same name in a class, but with different parameters.
- 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
- You can get input from console using Scanner object:
Scanner scanner = new Scanner(System.in);
- 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
- 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.
- if an element does not exist, a negative value will be returned.
- The array must be pre-sorted in increasing order, otherwise the results are
undefined. - 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.