3.9 KiB
3.9 KiB
- #CT2106 - Object-Oriented Programming
- Previous Topic: Introduction to Inheritance
- Next Topic: Abstraction & Polymorphism
- Relevant Slides:
-
Key Ideas in a Class Hierarchy
- The top of the hierarchy represents the most generic attributes & behaviours.
- The bottom (the leaves) represent the most specific attributes & behaviours.
- Each level inherits & customises the attributes & behaviours from the level above it.
- What is OOP Inheritance? #card
card-last-interval:: 4
card-repeats:: 2
card-ease-factor:: 2.7
card-next-schedule:: 2022-11-22T18:35:20.846Z
card-last-reviewed:: 2022-11-18T18:35:20.847Z
card-last-score:: 5
- OOP Inheritance is the means by which objects automatically receive features (fields) & behaviours (methods) from their super classes.
-
Java Class Hierarchy
- At the top of the Java Class Hierarchy is a class called
java.lang.Object
. - All classes inherit implicitly from
java.lang.Object
.- This means that a class doesn't have to specify explicitly that
java.lang.Object
is its superclass.
- This means that a class doesn't have to specify explicitly that
-
Rules of Class Hierarchy
- In Java, the variable type can be the superclass of the object.
- The variable type can be any superclass of the object, not just
java.lang.Object
.
- The variable type can be any superclass of the object, not just
- In Java, the variable type can be the superclass of the object.
- At the top of the Java Class Hierarchy is a class called
-
Explicit Inheritance
- All classes inherit methods implicitly from
java.lang.Object
.- Two common methods that are inherited from
java.lang.Object
:equals()
toString()
- Two common methods that are inherited from
- In every other case, you have to tell Java which classes are in a superclass relationship.
-
Steps
-
- Create the classes
- Inert the inheritance relationships.
- Insert the fields.
- Insert the methods.
- Override the necessary fields.
- Override necessary methods.
- Test by putting objects in an array & calling their behaviours.
-
-
Defining Inheritance #card
card-last-interval:: 3.45 card-repeats:: 2 card-ease-factor:: 2.46 card-next-schedule:: 2022-11-18T06:10:29.757Z card-last-reviewed:: 2022-11-14T20:10:29.758Z card-last-score:: 3- The keyword
extends
indicates the subclass to be extended (inherited from). - You must call the constructor of the superclass using the method call
super()
.- If the superclass constructor takes a parameter, then the call to
super()
must include a value of the parameter.
- If the superclass constructor takes a parameter, then the call to
- For example:
-
public class Bird extends Animal { boolean hasFeathers; // these fields aren't private. boolean hasWings; // we want these fields to be inherited boolean flies; // so we don't make the private. public Bird() { super(); // calls the constructor of its superclass - Animal colour = "black"; hsaFeathers = true; hasWings = true; flies = true; } }
-
- The keyword
- All classes inherit methods implicitly from
-
card-last-interval:: 4 card-repeats:: 2 card-ease-factor:: 2.7 card-next-schedule:: 2022-11-18T20:09:22.323Z card-last-reviewed:: 2022-11-14T20:09:22.324Z card-last-score:: 5abstract
#card- It may not make sense to have an object of a superclass type.
- For example, there is no object that is just an
Animal
orBird
and no more than that - all Animals are a specific subclass ofAnimal
.
- For example, there is no object that is just an
- ^^The Java keyword
abstract
allows you to specify which classes can be made into objects and which are used for inheritance purposes.^^- Adding the keyword
abstract
to the class definition tells Java that it can't make objects from this class. - However, an abstract class can still be used as a type of reference variable.
-
Bird bird = new Canary("John"); Animal animal = new Canary("Mary");
-
- Adding the keyword
- For example:
-
public abstract class Animal // doesn't allow objects of just type Animal
-
public abstract class Bird extends Animal
-
- It may not make sense to have an object of a superclass type.