Inheritance Unit 9
Me attempting to do the work and be responsible
- What is Inheritance?
- SuperClass
- Writing Constructors for Subclasses
- Hack 1
- Overriding Methods
- Hack 2
- Super Keyword
- Creating References Using Inheritance Hierarchies
- Polymorphism
- Hack 3
- Object Superclass
public class Car {
protected String brandName;
protected double range;
protected double doorNumber;
protected double maxSpeed;
// Constructor for the attributes present in the superclass
public Car(String brandName, double range, double doorNumber, double maxSpeed) {
this.brandName = brandName;
this.range = range;
this.doorNumber = doorNumber;
this.maxSpeed = maxSpeed;
}
public void gas () {
System.out.println("Go!");
}
public void brake () {
System.out.println("Stop!");
}
public void gearShift () {
System.out.println("Use the stick");
}
public void steer () {
System.out.println("turning left...");
}
public void horn () {
System.out.print("honking... ");
}
}
public class TeslaModelS extends Car {
// Additional attribute not present in the superclass
protected String hornSound;
// Constructor for Subclass
public TeslaModelS(String brandName, double range, double doorNumber, double maxSpeed, String hornSound) {
// We use the Superclass constructor for the shared attributes through the keyword "super"
super(brandName, range, doorNumber, maxSpeed);
// hornSound is not in the Superclass, so we add it separately in the constructor
this.hornSound = hornSound;
}
// We use override to change the functionality in the subclass of an existing method in the superclass
@Override
public void gearShift () {
System.out.println("Use the gear selector next to the wheel");
}
public void steer () {
System.out.println("turning right...");
}
// Here, we don't fully change the functionality of the existing horn method in the superclass
// Instead, we take all of the functionality of the superclass method, and then add on to it
public void horn () {
super.horn();
System.out.println(hornSound);
}
public static void main(String[] args) {
// 5 argument constructor
TeslaModelS modelS = new TeslaModelS("Tesla", 396, 4, 200, "eugh");
// Example of late binding
Car car = new TeslaModelS("Tesla", 396, 4, 200, "brrr");
// We can still use the methods from the child class, even though we didn't mention them in the subclass!
modelS.gas();
// Using the overridden method
modelS.gearShift();
modelS.steer();
// Using the method we added on to
modelS.horn();
car.horn();
}
}
TeslaModelS.main(null);
// Creating animal superclass and giving it two attributes
public class Animals{
protected String color;
protected double numberOfLegs;
// Creating animals constructor
public Animals(String color, double numberOfLegs){
this.color = color;
this.numberOfLegs = numberOfLegs;
}
}
// Creating wolf subclass and giving it claws attribute
public class Wolf extends Animals {
protected String hasClaws;
// Creating a constructor for subclass
public Wolf(String color, double numberOfLegs, String hasClaws){
super(color, numberOfLegs);
this.hasClaws = hasClaws;
}
public static void main(String[] args) {
Wolf Wolf = new Wolf("gray", 4, "true");
}
}
Wolf.main(null);
// Creating animals superclass and giving it two attributes
public class Animals{
protected String color;
protected double numberOfLegs;
// Creating animals constructor
public Animals(String color, double numberOfLegs){
this.color = color;
this.numberOfLegs = numberOfLegs;
}
public void tail(){
System.out.println("This animal does not have a tail.");
}
}
// Creating wolf subclass and giving it claws attribute
public class Wolf extends Animals {
protected String hasClaws;
// Creating a constructor for subclass
public Wolf(String color, double numberOfLegs, String hasClaws){
super(color, numberOfLegs);
// Third attribute
this.hasClaws = hasClaws;
}
// Utilizing override
@Override
public void tail() {
System.out.println("This animal does have a tail.");
}
public static void main(String[] args) {
System.out.println("The following displays information about a wolf species:");
Wolf Wolf = new Wolf("gray", 4, "true");
Wolf.tail();
}
}
Wolf.main(null);
Super Keyword
- super keyword --> use constructors in the superclass and methods in the superclass in a child class
- TeslaModelS --> super keyword can be found in constructor and the method horn
- When it says super(brandName, range, doorNumber, maxSpeed) in the constructor, super keyword is used to utilize super class constructor inside child class
- super.horn() calling horn() from parent class Car
Creating References Using Inheritance Hierarchies
- Type diagram: similar appearance to a family tree. A is superclass/head of the family. Descendants are subclasses
- public class A public class B extends A public class C extends B
- A reference refers to an object of the class or object of an inherited class
Polymorphism
- means "many forms" --> one thing in many ways through inheritance
- can have two different implementations through overriding methods
- This is known as Runtime Polymorphism
- Method overloading: two methods with same name but different arguments and functionalities
- Compile Time Polymorphism or Static Polymorphism
- Early binding: when compiler decides which method should be called
- Late binding: when method is decided at runtime
// Creating animals superclass
public class Animals{
protected String color;
protected double numberOfLegs;
public Animals(String color, double numberOfLegs){
this.color = color;
this.numberOfLegs = numberOfLegs;
}
public void tail(){
System.out.println("This animal does not have a tail.");
}
public void lifeSpan(){
System.out.println("Lifespan for this animal is unknown.");
}
}
// Creating wolf subclass and giving it attributes
public class Wolf extends Animals {
protected String hasClaws;
//Creating wolf constructor
public Wolf(String color, double numberOfLegs, String hasClaws){
super(color, numberOfLegs);
this.hasClaws = hasClaws;
}
// Utilizing override
@Override
public void tail() {
System.out.println("This animal does have a tail.");
}
//Utilizing override
@Override
public void lifeSpan() {
System.out.println("This animal's average lifespan ranges from 14 to 16 years of age.");
}
public static void main(String[] args) {
System.out.println("The following displays information about a wolf species:");
Wolf Wolf = new Wolf("gray", 4, "true");
Wolf.tail();
Wolf.lifeSpan();
}
}
//Creating fish subclass
public class Fish extends Animals {
// Fish subclass constructor
public Fish(String color, double numberOfLegs){
super(color, numberOfLegs);
}
@Override
public void tail() {
System.out.println("This animal does have a tail.");
}
public void lifeSpan(int a, int b) {
System.out.println("This animal's average lifespan ranges from " + a + " to " + b + " years of age.");
}
public static void main(String[] args) {
System.out.println("The following displays information about a fish species:");
Fish Fish = new Fish("silver", 0);
Fish.tail();
Fish.lifeSpan(6, 14);
}
}
Wolf.main(null);
Fish.main(null);
Object Superclass
- Important things to know
- Object class is superclass of all other classes in java
- Object is part of the java.lang package --> known for imports
- Important Object class methods include: boolean equals(Object x) & String toString()
- Object class' subclass override the equals and toStrong methods
- toString Method
- Prints out the attributes of an object
- Converts string object into a string
- equals Method
- Compares two strings
- Returns a boolean value of true if equal...otherwise returns false