Notes
- Inheritance is the ability to inherit characteristics from super class to sub classes
- Reduces risk of error and reduces redundancy
- extends key word used to inherit characteristics from a class
- Constructors in a sub class needs to have at least the same definition as the old class
- super() to call supper class's constructor which must have all relevant parameters passed into it
- @Override to overriding methods of a super class and makes it readable
- Tree called inheritance hierarchies where lower subclasses inherit upper super classes in the hierarchy
- Polymorphism allows methods to have multiple forms for code reusability with different forms/implementations
public class WorldCup{
private int wins;
private int losses;
private int draws;
public WorldCup(int wins, int losses, int draws){
this.wins = wins;
this.losses = losses;
this.draws = draws;
}
public String toString(){
return ( "{ \"wins\": " +this.wins+ ", " + "\"losses\": " +this.losses+ ", " + "\"draws\": " +this.draws+ " }" );
}
public static void main(String[] args) {
Argentina argentina = new Argentina(5, 1, 0);
System.out.println("Argentina: " + argentina);
Croatia croatia = new Croatia(3, 1, 2);
System.out.println("Croatia: " + croatia);
France france = new France(4, 1, 0);
System.out.println("France: " + france);
Morocco morocco = new Morocco(4, 0, 1);
System.out.println("Morocco: " + morocco);
Brazil brazil = new Brazil(3, 2, 0);
System.out.println("Brazil: " + brazil);
}
}
public class Argentina extends WorldCup{
public Argentina(int wins, int losses, int draws){
super(wins, losses, draws);
}
}
public class Croatia extends WorldCup{
public Croatia(int wins, int losses, int draws){
super(wins, losses, draws);
}
}
public class France extends WorldCup{
public France(int wins, int losses, int draws){
super(wins, losses, draws);
}
}
public class Morocco extends WorldCup{
public Morocco(int wins, int losses, int draws){
super(wins, losses, draws);
}
}
public class Brazil extends WorldCup{
public Brazil(int wins, int losses, int draws){
super(wins, losses, draws);
}
}
WorldCup.main(null);
- Add a getAge method in the Person super class
- Create a new subclass Student with additional members of your choice to personalize the Student class
- Create a new subclass Teacher with additional members of your choice
- Override the toString method using the @Override to print a Student and teacher object with new members
- Print the student and teacher.
import java.util.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
public class Person {
public String name;
public LocalDate birthday;
public Person (String name, LocalDate birthday){
this.name = name;
this.birthday = birthday;
}
public String getName(){
return name;
}
public int getAge(){
return Period.between(this.birthday, LocalDate.now()).getYears();
}
public String toString() {
return ( "{ \"name\": " +this.name+ ", " + "\"birthday\": " +this.birthday+ " }" );
}
}
public class Student extends Person {
public int grade;
public double gpa;
public Student (String name, LocalDate birthday, int grade, double gpa) {
super(name, birthday);
this.grade = grade;
this.gpa = gpa;
}
public int getGrade(){
return grade;
}
public double getGPA(){
return gpa;
}
@Override
public String toString(){
return ( "{ \"name\": " +this.name+ ", " + "\"birthday\": " +this.birthday+ ", " + "\"grade\": " +this.grade+ ", " + "\"gpa\": " +this.gpa+ " }" );
}
}
public class Teacher extends Person {
private String subject;
public Teacher (String name, LocalDate birthday, String subject){
super(name, birthday);
this.subject = subject;
}
@Override
public String toString(){
return ( "{ \"name\": " +this.name+ ", " + "\"birthday\": " +this.birthday+ ", " + "\"subject\": " +this.subject+ " }" );
}
}
public class Main{
public static void main(String[] args){
LocalDate dob = LocalDate.parse("2000-01-01");
Person person = new Person("Person", dob);
System.out.println(person.toString());
Student student = new Student("Student", dob, 11, 4.0);
System.out.println(student.toString());
Teacher teacher = new Teacher("Teacher", dob, "Computer Science");
System.out.println(teacher.toString());
}
}
Main.main(null);