Unit 09 Mon - Inheritance
Notes + Homework
- constructor is a special method that is used to create and initialize an object
- When creating a subclass, you typically want to reuse the constructor from the parent class (also known as the "superclass") and add some additional functionality
public class Animal {
// Constructor for the Animal class
public Animal() {
// Initialize the object
}
}
public class Dog extends Animal {
// Constructor for the Dog class
public Dog() {
// Call the constructor of the superclass
super();
// Initialize the object
}
}
- Possible to call a specific constructor of the superclass by specifying the parameters
/**
* In this example using inheritance allows us to inherit the salary of the programmer by extending the superclass of employee
*/
class Person {
private String name;
public Person(String theName) {
this.name = theName;
}
public String getName() {
return name;
}
public boolean setName(String theNewName) {
if (theNewName != null) {
this.name = theNewName;
return true;
}
return false;
}
}
public class Employee extends Person {
private int id;
public static int nextId = 1;
public Employee(String theName) {
super(theName);
id = nextId;
nextId++;
}
public int getId() {
return id;
}
public static void main(String[] args) {
Employee emp = new Employee("Yash");
System.out.println(emp.getName());
System.out.println(emp.getId());
}
}
Employee.main(null);
/**
* Example of polymorphism in Java
*/
public class Sport {
// action() method instantiated
public void action() {
System.out.println("Physical activity");
}
}
// Subclass Soccer
class Soccer extends Sport {
// Soccer implementation of the action() method
public void action() {
System.out.println("Kick the ball");
}
}
// Subclass Basketball
class Basketball extends Sport {
// Basketball implementation of the action() method
public void action() {
System.out.println("Shoot the ball");
}
}
// Subclass Baseball
class Baseball extends Sport {
// Baseball implementation of the action() method
public void action() {
System.out.println("Hit a home run");
}
}
// Main class for running the code
class Main {
public static void main(String[] args) {
// Creating objects with the same reference type but different object types. The classes above follow a class hierarchy.
Sport sport = new Sport();
Sport sport1 = new Soccer();
Sport sport2 = new Basketball();
Sport sport3 = new Baseball();
sport.action();
sport1.action();
sport2.action();
sport3.action();
}
}
Main.main(null);
/**
* Overriding methods
*/
class Vehicle {
void run(){System.out.println("Vehicle is running");}
}
//Creating a child class
class Bike extends Vehicle {
public static void main(String args[]) {
//creating an instance of child class
Bike obj = new Bike();
//calling the method with child class instance
obj.run();
}
}
Bike.main(null);
public class Team {
public int score;
public int wins;
public int all_goals;
public Team(int score, int wins, int all_goals) {
this.score = score;
this.wins = wins;
this.all_goals = all_goals;
}
public void score() {
this.score += 1;
}
public void bestplayer() {
System.out.println("Best player: Unknown");
}
public void out() {
System.out.println("Team score: " + this.score);
System.out.println("Team wins: " + this.wins);
System.out.println("Team all goals: " + this.all_goals);
bestplayer();
}
}
public class France extends Team {
public France(int score, int wins, int all_goals) {
super(score, wins, all_goals);
}
public void score() {
this.score += 1;
}
public void bestplayer() {
System.out.println("Best player: Kylian Mbappé");
}
}
public class Argentina extends Team {
public Argentina(int score, int wins, int all_goals) {
super(score, wins, all_goals);
}
public void score() {
this.score += 1;
}
public void bestplayer() {
System.out.println("Best player: Lionel Messi");
}
}
public class Croatia extends Team {
public Croatia(int score, int wins, int all_goals) {
super(score, wins, all_goals);
}
public void score() {
this.score += 1;
}
public void bestplayer() {
System.out.println("Best player: Luka Modrić");
}
}
public class Morocco extends Team {
public Morocco(int score, int wins, int all_goals) {
super(score, wins, all_goals);
}
public void score() {
this.score += 1;
}
public void bestplayer() {
System.out.println("Best player: Hakim Ziyech");
}
}
public class Portugal extends Team {
public Portugal(int score, int wins, int all_goals) {
super(score, wins, all_goals);
}
public void score() {
this.score += 1;
}
public void bestplayer() {
System.out.println("Best player: Cristiano Ronaldo");
}
}
Team arg = new Argentina(0, 2, 12);
Team por = new Portugal(0, 1, 12);
Team frc = new France(0, 2, 11);
Team cro = new Croatia(0, 1, 6);
Team mor = new Morocco(0, 1, 5);
arg.out();
frc.out();
por.out();
cro.out();
mor.out();
Part 2
- 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.
public class Person {
protected String name;
protected String birthday;
public Person (String name, String birthday) {
this.name = name;
this.birthday = birthday;
}
public String getName() {
return name;
}
public int getAge() {
return 2022 - Integer.parseInt(birthday);
}
@Override
public String toString() {
return "Person (name: " + name + ", birthday: " + birthday + ")";
}
}
public class Student extends Person {
private int grade;
private double gpa;
private String extracurricular;
public Student (String name, String birthday, int grade, double gpa, String extracurricular) {
super(name, birthday);
this.grade = grade;
this.gpa = gpa;
this.extracurricular = extracurricular;
}
// return gpa
public double getGPA() {
return gpa;
}
public String extracurricular() {
return extracurricular;
}
// return grade
public int getGrade() {
return grade;
}
@Override
public String toString() {
return "Student (name: " + name + ", birthday: " + birthday + ", extracurricular: " + extracurricular + ", gpa: " + gpa + ", grade: " + grade + ")";
}
}
public class Teacher extends Person {
private String subject;
private int tenureYears;
private String degree;
public Teacher (String name, String birthday, String subject, int tenureYears, String degree) {
super(name, birthday);
this.subject = subject;
this.tenureYears = tenureYears;
this.degree = degree;
}
// return subject
public String getSubject() {
return subject;
}
// return yearsOfExperience
public int getTenure() {
return tenureYears;
}
// return degree
public String getDegree() {
return degree;
}
@Override
public String toString() {
return "Teacher (name: " + name + ", birthday: " + birthday + ", subject: " + subject + ", tenureYears: " + tenureYears + ", degree: " + degree + ")";
}
}
Person wb = new Person("Warren Buffett", "1930");
System.out.println(wb.toString());
Person ys = new Student("yash", "2005", 12, 4.2, "Karate");
System.out.println(ys.toString());
Person jm = new Teacher("John Mortensen", "1959", "Computer Science", 22, "Bachelors of Science in Computer Science");
System.out.println(jm.toString());