Rohan G CollegeBoard Unit 9 Inheritance
Week 16 Unit 9 Inheritance Lesson and Homework
Inheritance
- The capability of a class to derive properties and characteristics from another class
Creating Classes and Subclasses
- For the example not using inheritance for each of the three classes we have to rewrite the three methods
- By creating a superclass vehicle which is extended by the three subclasses we reduce risk of error and redundancy and simplifies our code
public class Vehicle {
public int fuelamount;
public int capacity;
public boolean applyBrakes;
public Vehicle (int fuelamount, int capacity, boolean applyBrakes) {
this.fuelamount;
this.capacity;
this.applyBrakes;
}
}
public class Bus extends Vehicle {
}
public class Car extends Vehicle {
}
public class Truck extends Vehicle {
}
Writing constructors for 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.
Creating References using Inheritance Hierarchies
- Superclasses and subclasses are generally organized into a single root tree structure called an inheritance hierarchy. In this type of hierarchy, attributes and methods that are associated with a specific class are inherited by lower levels of the hierarchy.
- When working with a hierarchy of subclasses and superclasses, the object type and reference type can be different when instantiating an object variable.
- Example: Fruit fruit1 = new Apple(); In this case, the reference type is declared with the superclass, while the object type is declared with the subclass.
Object superclass
- The object superclass is located at the top of every class hierarchy. This means all of the other classes in a hierarchy can use the attributes and methods of the object superclass through inheritance. It is a part of the built-in java.lang package.
- The object superclass contains common, useful methods such as equals() and toString(). The equals() method compares two objects; it is often used to check for equality between 2 String objects, comparing each character of both strings to check if they are the same.
- The toString() method returns a String representation of a given object.
public class WorldCup {
protected String country;
protected String bestplayer;
public WorldCup (String country, String bestplayer) {
this.country = country;
this.bestplayer = bestplayer;
}
public String getCountry(){
return country;
}
public String getBestPlayer(){
return bestplayer;
}
@Override
public String toString() {
return "WorldCup (country: " + country + ", bestplayer: " + bestplayer + ")";
}
public static void main(String[] args) {
WorldCup Qatar = new WorldCup("Qatar", "who knows");
System.out.println(Qatar.toString());
}
}
public class Morocco extends WorldCup {
private int goalsscored;
private int goalsagainst;
public Morocco (String country, String bestplayer, int goalsscored, int goalsagainst){
super(country, bestplayer);
this.goalsscored = goalsscored;
this.goalsagainst = goalsagainst;
}
public int getgoalsscored() {
return goalsscored;
}
public int getgoalsagainst() {
return goalsagainst;
}
@Override
public String toString() {
return "WorldCup (country: " + country + ", bestplayer: " + bestplayer + ", Goals Scored: " + goalsscored + ", Goals Against: " + goalsagainst + ")";
}
public static void main(String[] args) {
WorldCup Morocco1 = new Morocco("Morocco", "En-Nesyri", 6, 0);
System.out.println(Morocco1.toString());
}
}
public class Croatia extends WorldCup {
private int goalsscored;
private int goalsagainst;
public Croatia (String country, String bestplayer, int goalsscored, int goalsagainst){
super(country, bestplayer);
this.goalsscored = goalsscored;
this.goalsagainst = goalsagainst;
}
public int getCroatiaScored() {
return goalsscored;
}
public int getgoalsagainst() {
return goalsagainst;
}
@Override
public String toString() {
return "WorldCup (country: " + country + ", bestplayer: " + bestplayer + ", Goals Scored: " + goalsscored + ", Goals Against: " + goalsagainst + ")";
}
public static void main(String[] args) {
WorldCup Croatia1 = new Croatia("Croatia", "Modric", 8, 2);
System.out.println(Croatia1.toString());
}
}
public class France extends WorldCup {
private int goalsscored;
private int goalsagainst;
public France (String country, String bestplayer, int goalsscored, int goalsagainst){
super(country, bestplayer);
this.goalsscored = goalsscored;
this.goalsagainst = goalsagainst;
}
public int getFranceScored() {
return goalsscored;
}
public int getgoalsagainst() {
return goalsagainst;
}
@Override
public String toString() {
return "WorldCup (country: " + country + ", bestplayer: " + bestplayer + ", Goals Scored: " + goalsscored + ", Goals Against: " + goalsagainst + ")";
}
public static void main(String[] args) {
WorldCup France1 = new France("France", "Mbappe", 13, 6);
System.out.println(France1.toString());
}
}
public class Argentina extends WorldCup {
private int goalsscored;
private int goalsagainst;
public Argentina (String country, String bestplayer, int goalsscored, int goalsagainst){
super(country, bestplayer);
this.goalsscored = goalsscored;
this.goalsagainst = goalsagainst;
}
public int getArgentinaScored() {
return goalsscored;
}
public int getgoalsagainst() {
return goalsagainst;
}
@Override
public String toString() {
return "WorldCup (country: " + country + ", bestplayer: " + bestplayer + ", Goals Scored: " + goalsscored + ", Goals Against: " + goalsagainst + ")";
}
public static void main(String[] args) {
WorldCup Argentina1 = new Argentina("Argentina", "Messi", 10, 5);
System.out.println(Argentina1.toString());
}
}
WorldCup.main(null);
Morocco.main(null);
France.main(null);
Croatia.main(null);
Argentina.main(null);
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 additiona 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;
protected int age;
public Person (String name, String birthday, int age){
this.name = name;
this.birthday = birthday;
this.age = age;
}
public String getName(){
return name;
}
public String getBirthday(){
return birthday;
}
public int getAge() {
return age;
}
}
public class Student extends Person {
private int grade;
private double gpa;
public Student (String name, String birthday, int age, int grade, double gpa) {
super(name, birthday, age);
this.grade = grade;
this.gpa = gpa;
}
public int getGrade(){
return grade;
}
public double getGPA(){
return gpa;
}
@Override
public String toString(){
return "Person (name: " + name + ", grade: " + grade + ", birthday: " + birthday + ", Age: " + age + ", GPA: " + gpa + ")";
}
}
public class Teacher extends Person {
private String classtaught;
private int yearsteaching;
public Teacher (String name, String birthday, int age, String classtaught, int yearsteaching) {
super(name, birthday, age);
this.classtaught = classtaught;
this.yearsteaching = yearsteaching;
}
public String getClassTaught(){
return classtaught;
}
public int getYearsTeaching(){
return yearsteaching;
}
@Override
public String toString(){
return "Person (name: " + name + ", Class Taught: " + classtaught + ", birthday: " + birthday + ", Age: " + age + ", Years Teaching: " + yearsteaching + ")";
}
}
Person John = new Teacher("John Mortensen", "idk", 65, "Computer Science", 5);
System.out.println(John.toString());
Person Rohan = new Student("Rohan Gaiks", "March 2", 16, 11, 4.0);
System.out.println(Rohan.toString());
System.out.println(Rohan.getAge());