Unit 9
unit 9 inheritance
- superclass
- extends charactersitcs down to subclasses
- Allows reusing the code and adding attributes if needed in every subclass.
- A subclass can also become a super class by extending it again in another subclass.
- same goes for a constructor in which, the keyword “super” is used to call the super class constructor and can add additional attributes for personalization.
- override used for overriding methods
- superclasses and subclasses organized into inherticane heiracrchy
- polymorphism
Vocab
- Constructors - initialize the attributes for an object
- is a tool used to describe the time complexity of algorithms. It calculates the time taken to run an algorithm as the input grows. In other words, it calculates the worst-case time complexity of an algorithm. Big O Notation in Data Structure describes the upper bound of an algorithm’s runtime.
- polymorphism is the capability of a method to do different things depending on the object it is acting upon.
public class VehicleTester
{
public static void main(String[] args)
{
// Create 3 Vehicle objects
Vehicle tesla = new Car("Tesla", "Model 3");
Vehicle trek = new Bike("White", "Mountain Bike");
Vehicle boat = new Vehicle("Boat", true, 0);
// Add to a Vehicle array
Vehicle[] transportation = new Vehicle[3];
transportation[0] = tesla;
transportation[1] = trek;
transportation[2] = boat;
// Call the getName method for each
// This will compile since getName is in the Vehicle class
// Each object will use it's own method.
for (int i = 0; i < transportation.length; i++){
System.out.println("Name: " + transportation[i].getName());
}
}
}
public class WorldCup{
public int wins;
public int losses;
public int players;
public int goals;
public WorldCup(int wins, int losses, int players, int goals){
this.wins = wins;
this.losses = losses;
this.players = players;
this.goals = goals;
}
public String toString(){
return ("Wins: " + this.wins + ", Losses: " + this.losses + ", Players: " + this.players + ", Goals:" + this.goals);
}
}
public class France extends WorldCup{
public France(int wins, int losses, int players, int goals){
super(wins, losses, players, goals);
}
}
public class Croatia extends WorldCup{
public Croatia(int wins, int losses, int players, int goals){
super(wins, losses, players, goals);
}
}
public class Morocco extends WorldCup{
public Morocco(int wins, int losses, int players, int goals){
super(wins, losses, players, goals);
}
}
public class Brazil extends WorldCup{
public Brazil(int wins, int losses, int players, int goals){
super(wins, losses, players, goals);
}
}
public class Argentina extends WorldCup{
public Argentina(int wins, int losses, int players, int goals){
super(wins, losses, players, goals);
}
}
public class Output{
public static void main(String[] args){
France france = new France(31, 22, 12, 9);
System.out.println("France " + france);
Croatia croatia = new Croatia(24, 18, 19, 8);
System.out.println("Croatia " + croatia);
Morocco morocco = new Morocco(23, 78, 32, 2);
System.out.println("Morocco " + morocco);
Brazil brazil = new Brazil(108, 3, 21, 90);
System.out.println("Brazil " + brazil);
Argentina argentina = new Argentina(72, 14, 34, 26);
System.out.println("Argentina " + argentina);
}
}
Output.main(null);
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.Date;
public class Person{
private String name;
private String birthday;
private int age;
public Person (String name, String birthday){
this.name = name;
this.birthday = birthday;
}
public int getAge(){
LocalDate date = LocalDate.parse(this.birthday);
return Period.between(date, LocalDate.now()).getYears();
}
public String getBirthday(){
return birthday;
}
public String getName(){
return name;
}
public String toString(){
return("Name: " + this.getName() + ", Birthday: " + this.getBirthday() + ", Age: " + this.getAge());
}
}
public class Student extends Person {
private int grade;
private double gpa;
private int numOfClasses;
public Student (String name, String birthday, int grade, double gpa, int numOfClasses) {
super(name, birthday);
this.grade = grade;
this.gpa = gpa;
this.numOfClasses = numOfClasses;
}
public int getGrade(){
return grade;
}
public double getGPA(){
return gpa;
}
public int getNumOfClasses(){
return numOfClasses;
}
@Override
public String toString(){
return ( "Name: " + this.getName() + ", Birthday: " + this.getBirthday() + ", Age: " + this.getAge() + ", Grade: " + this.getGrade() + ", GPA: " + this.getGPA() + ", Number of classes: " + this.getNumOfClasses());
}
}
public class Teacher extends Person {
private String subject;
private int age;
public Teacher (String name, String birthday, int age, String subject){
super(name, birthday);
this.subject = subject;
}
public String getSubject(){
return subject;
}
@Override
public String toString(){
return("Name: " + this.getName() + "," + " Age: " + this.getAge() + ", subject: " + this.getSubject());
}
}
public class Homework{
public static void main(String[] args) {
Student joe = new Student("joe", "2003-10-11", 11, 4.0, 12);
System.out.println(joe.toString());
Teacher marge = new Teacher("marge", "1968-02-12", 54, "math" );
System.out.println(marge.toString());
}
}
Homework.main(null);