9.1 What is inheritance?
- Inhertiance is like a family, except the kids only have one parent instead of two
- For example: ```java class Mom{
} class Son extends Mom{
} class Daughter extends Mom{
}
In this example, the Son and Daughter inherits the Mom, meaning it inherit the components of the mother. This makes the "Son" and "Daughter" classes the ____ of the "Mom" class as they inherit the "Mom" class components and the "Mom" class the _____.
## 9.2 Using the Super keyword for Constructors
- One keyword to know is the super keyword
- The super keyword allows the subclass to store key variables in the main class's ____ (also known as the super class)
- Example below:
```Java
public class Vehicle { //This is the Superclas, it inherits the key variables for its subclasses
public String Name; //They don't have to be public, but I just put public word for fun
public String Sound;
public int creation;
public int Mph;
public Vehicle(String name, int dateMade, String sound, int mph){ //Similar to the constructor used in Javascript. It maintains values within this superclass
Name = name;
Sound = sound;
creation = dateMade;
Mph = mph;
}
}
public class Car extends Vehicle {
public Car(String name, int dateMade, String sound, int mph){
super(name, dateMade,sound, mph); //Uses the superclass's constructor to store the key variables for the Car subclass
}
public void Information(){
System.out.println(super.Sound);
System.out.println("I am the " + super.Name);
System.out.println("I was made in " + super.creation);
System.out.println("I move at " + super.Mph +" miles per hour");
}
}
public class Test {
public static void main(String[] args){
Car Tesla = new Car("Tesla", 2003, "VROOM!", 200);
Tesla.Information();
}
}
Test.main(null);
VROOM!
I am the Tesla
I was made in 2003
I move at 200 miles per hour
9.4 Using Super keyword for Methods
- Why only use super for constructors when you can use them for methods too?
- With the super key word, not only can you store variables, but also store methods
class Animal{
public void Introduction(String name){
System.out.println("I am a " + name);
}
}
class Dog extends Animal{
public void Woof(){
super.Introduction("Dog");//Inherits the introduction method in the Animal Class, then introduces itself as a dog
System.out.println("Woof!"); //Does its own thing
}
}
class Cow extends Animal{
public void Moo(){
super.Introduction("Cow");//Inherits the introduction method in the Animal Class, then introduces itself as a cow
System.out.println("MOOOO!");//Does its own thing
}
}
class Test{
public static void main(String[] args){
Dog dog = new Dog();
Cow cow = new Cow();
dog.Woof();
cow.Moo();
}
}
Test.main(null);
I am a Dog
Woof!
I am a Cow
MOOOO!
9.5 Inheritance Hierarchies
public class Company{
public String Name;
public Company(String name){
this.Name = name;
}
}
public class Workplace extends Comapny{
public String name;
public Workplace(String companyName, String Name){
super(companyName);
this.name = Name;
}
}
public class Worker extends Workplace{
public Worker(String companyName, String, workplaceName)
}
public class Test{
public static void main(String[] args){
}
}
Hacks
- Popcorn Hacks (0.2): Fill in all of the blanks. Miss one blank, you get no points for popcorn hacks
- FRQ Hacks (0.7): Make a complex FRQ that involves everything we taught
- Challenge (0.1): Make a minihack that uses everything we taught and it must run through main and uses input and output. The more complex, the higher the score.