Vocab
- inheritance, extends
- a way for attributes and methods to be inherited between clases
- extends is what allows you to bring those attributes
- subclass constuctor, super Keyword 2
- A subclass inherits all the members from the superclass while the constructor of the superclass can be invoked from the subclass
- Super keyword refers to superclass objects; it is used to call superclass methods and to access the superclass costructor
- Overloading a method, same name different parameters
- Overloading a method in java is a feature that allows a class to have more than one method with the same name, but with different parameters
- Instead of defining two methods that do the same thing, it is better to overload one
- Polymorphism: any of overloading, overriding, late binding
- Polymorphism is the ability of a class to provide different implementations of a method depending on the type of object that is passed to the method
- Allows us to perform the same action in many different ways
Inheritance
- the capability of a class to derive properties and characteristics from another class
- without inheritance, you have to rewrite methods in each different class
- with inheritance, create a superclass vehicle which is extended by subclasses (don't have to rewrite methods)
Syntax
- public class A {...}
- public class B extends A {...}
Overriding
- @Override is used to give different implementations to method of superclass
Inheritance Hierachies
- superclasses and subclasses are gernally organized into a single root tree strucutre called an inheritance hierachy
- lower levels of hierchy inherit attributes and methods from upper level
- object type and refernce type can be different when instantiating an object variable
Polymorphism
- utilized when a method is implemented to perform different tasks in different classes
- allows method to take on multiple "forms" or implementations based on specific object it is acting upon
HW
Part 1
- Create a world cup superclass with properties of your choice and subclasses for five teams which inherits those properties
- Write a constructor for one of those subclasses
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.
Part 1
public class WC{
private int goals;
private int wins;
public WC(int goals, int wins) {
this.goals = goals;
this.wins = wins;
}
public String toString(){
return(" Goals Scored in WC: " + this.goals + ", World Cup Wins: " + this.wins);
}
public static void main(String[] args) {
Argentina argentina = new Argentina(12, 2);
System.out.println("Argentina: " + argentina);
Brazil brazil = new Brazil(8, 5);
System.out.println("Brazil: " + brazil);
France france = new France(11, 2);
System.out.println("France: " + france);
Portugal portugal = new Portugal(12, 0);
System.out.println("Portugal: " + portugal);
Morocco morocco = new Morocco(5, 0);
System.out.println("Morocco: " + morocco);
}
}
public class Argentina extends WC {
public Argentina(int goals, int wins) {
super(goals, wins);
}
}
public class Brazil extends WC {
public Brazil(int goals, int wins) {
super(goals, wins);
}
}
public class France extends WC {
public France(int goals, int wins) {
super(goals, wins);
}
}
public class Portugal extends WC {
public Portugal(int goals, int wins) {
super(goals, wins);
}
}
public class Morocco extends WC {
public Morocco(int goals, int wins) {
super(goals, wins);
}
}
WC.main(null);
public class WC{
private int goals;
private int wins;
public WC(int goals, int wins) {
this.goals = goals;
this.wins = wins;
}
public String toString(){
return(" Goals Scored in WC: " + this.goals + ", World Cup Wins: " + this.wins);
}
public static void main(String[] args) {
WC argentina = new WC(12, 2);
System.out.println("Argentina: " + argentina);
WC brazil = new WC(8, 5);
System.out.println("Brazil: " + brazil);
WC france = new WC(11, 2);
System.out.println("France: " + france);
WC portugal = new WC(12, 0);
System.out.println("Portugal: " + portugal);
WC morocco = new WC(5, 0);
System.out.println("Morocco: " + morocco);
}
}
WC.main(null);
public class Person {
public String name;
public String birthday;
public int age;
public Person (String name, String birthday, int age){
this.name = name;
this.birthday = birthday;
this.age = age;
}
public int getAge(){
return age;
}
public String toString(){
return "Name: " + this.name + ", Age: " + this.getAge();
}
}
public class Student extends Person {
private int grade;
private double gpa;
private int aps;
public Student (String name, String birthday, int age, int grade, double gpa, int aps) {
super(name, birthday, age);
this.grade = grade;
this.gpa = gpa;
this.aps = aps;
}
@Override
public String toString() {
return "Name: " + this.name + ", birthday: " + this.birthday + ", age: " + this.age + ", grade: " + this.grade +", gpa: " + this.gpa +", APs: " + this.aps;
}
}
public class Alice extends Student{
public Alice(String name, String birthday, int age, int grade, double gpa, int aps){
super(name, birthday, age, grade, gpa, aps);
}
public static void main(String[] args) {
Alice alice = new Alice("Alice", "2005-01-30",17, 12, 3.9, 12);
System.out.println("Student: " + alice.toString());
}
}
public class Teacher extends Person {
private String subject;
private int years;
public Teacher (String name, String birthday, int age, String subject, int years) {
super(name, birthday, age);
this.subject = subject;
this.years = years;
}
@Override
public String toString() {
return "Name: " + this.name + ", birthday: " + this.birthday + ", age: " + this.age + ", subject: " + this.subject +", years teaching: " + this.years ;
}
}
public class Smith extends Teacher{
public Smith(String name, String birthday, int age, String subject, int years){
super(name, birthday, age, subject, years);
}
public static void main(String[] args) {
Smith smith = new Smith("Mr. Smith", "1982-03-14", 40, "Math", 6);
System.out.println("Teacher: " + smith.toString());
}
}
Alice.main(null);
Smith.main(null);