Week6
[!TIP] ๐ ๆธฉ้ฆจๆ็คบ๏ผๆฌๅจ็ปไน ไปฃ็ ๅจ
../src/Week6ไธญ ๐ ็นๆ่ทณ่ฝฌ
Introduction to Inheritance and "Is a" Relationship
review๏ผencapsulationใdata hiding Association relationship๏ผhas
Inheritance
A superclass is a more generalized class
A subclass a more specialized class
GradedActivity
- score : double
โ | |
FinalExam
- numQuestions : int - pointsEach : double - numMissed : int
public class Car extends Vehicle {
}[!NOTE] Inheritance can only go up

Subclasses and Superclasses
The more general class is known as the superclass
EntityThe more specialized class is known as the subclass
HobbitAn instance of the subclass can be used whenever an instance of the superclass is required, e.g.
[!NOTE] A subclass in turn can have its own subclass, and so on.
The extends keyword
extends keywordThe relationship between a superclass and subclass is declared with the use of the
extendskeyword.A class may have one, many or no subclasses.
A class may have only one superclass.
No multiple inheritance!
The
thiskeyword
[!TIP] Remember: within a class, the keyword
thisis a reference to the instance of the class and allows us to refer to the attribute of the class rather than the passed-in parameter.
Inheritance and constructors
The subclass requires its own constructor.
This constructor will always make a call to a constructor of its superclass.
You can make this call explicit using the keyword
super.If you do not call the superclass constructor explicitly then Java will try to call a superclass constructor that takes no arguments โ if it canโt find one then it will report an error.
[!NOTE] If we donโt write a constructor for the class, Java will create one for us. It will take no parameters, and assign a null value to name and symbol. Note that Java only does this if we havenโt written a constructor ourselves.
[!NOTE] We do not write the superclass constructor. However, the superclass has a constructor taking no parameters. Java will call it for us. Note that if there wasnโt a superclass constructor with no parameters then Java would report an error.
Inheritance and overriding
The subclass can also have methods that override methods of the superclass, with the same name & parameters
Overriding is an example of polymorphism. Thatโs were you have two methods of the same name that behave differently.
use keyword @Override
Access Control
A method or attribute declared public will be available to/modifiable by all subclasses.
A method or attribute declared private is not modifiable or directly accessible by subclasses. In that case public "getter" and "setter" methods like public int getX() provide access.
Access Specifiers
default (no modifier)
โ
โ
ร
ร
Public
โ
โ
โ
โ
Protected
โ
โ
โ
ร
Private
ร
ร
ร
ร
Overriding move
moveOverriding toString
toStringPolymorphism
The method called depends on which class an object is an instance of, e.g.
will result in the method from the Hobbit class being called when
is called, because entity is an Entity object.
Polymorphism is an ability to change what a method does depending on the type of object. Both overloading and overriding are examples of polymorphism.
Overriding Superclass Methods
Recall that a methodโs signature consists of:
the methodโs name
the data types of the methodโs parameters in the order that they appear.
A subclass method that overrides a superclass method must have the same signature as the superclass method.
An object of the subclass invokes the subclassโs version of the method, not the superclassโs.
Overriding and overloading Methods
There is a distinction between overloading a method and overriding a method.
Overloading is when a method has the same name as one or more other methods, but with a different signature.
When a method overrides another method, however, they both have the same signature. Which one is used is determined by the object that invokes that method.
Work on exercise 6
Object
Object is the supersupersuper... class of every single class. Everything extends Object (though you donโt have to write that explicitly!)
The Object class provides various useful methods to every class, which include
boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.
String toString()
return a string with information about this object.
Since every class extends Object we can override these methods.
See also the Java API documentation: Overview (Java Platform SE 6)
Chains of Inheritance
A superclass can also be derived from another class.

Abstract classes
Sometimes we do not want to create direct instances of a superclass. For example we might want to insist that all instances of the Entity class are instances of some particular subclass of Entity โ for example a Wizard or Hobbit. We can achieve this by making the class abstract. We do this by putting the word abstract in its declaration.
A class that is not abstract is said to be concrete.(Assume class Hobbit is a subclass of class Entity)
Abstract methods
An abstract class may have abstract methods.
An abstract method is declared, but not implemented.
Abstract classes in UML version 1
You can indicate that a class or method is abstract by writing its name in italics.

Uses of inheritance
If classes are arranged into inheritance then we can write methods that will work for any subclass of a given superclass.
Casting
You can cast objects of one type into another, so long as the types are compatible. e.g. if Wizard and Hobbit are subclasses of Entity
Uses of inheritance
If classes are arranged into inheritance then we create lists whose elements must be subclasses of a given superclass, but which can be of any subclass.
[!TIP] We can add objects of any subclass of Entity to the list. But we canโt add any object that is not an instance of some Entity subclass.
Why use Inheritance?
Avoid writing shared code more than once
Useful for the lazy programmerโs toolbox
Reduces the opportunities for error.
Useful for helping to structure programs
There are other features of object-oriented programming languages that help with this too
Make explicit the relationships between different types of objects
Can extend library classes with extra features
Last updated