Attributes and methods can be inherited from one class to another
Use the extends
keyword
Create a class called Person
with the following instance variables:
What public and private methods?
Make sure you do validation for setter methods.
import java.time.LocalDate;
public class Person {
private String firstName;
private String familyName;
private int birthYear;
private int birthMonth;
private int birthDay;
public Person(String first, String family) {
firstName = first;
familyName = family;
}
public void setBirthDate(int year, int month, int day) {
LocalDate today = LocalDate.now();
if (year > 1900 && year <= today.getYear()) birthYear = year;
if (month > 0 && month <= 12) birthMonth = month;
if (day > 0 && day <= 31) birthDay = day;
}
public String getBirthDate() {
String month;
if (birthMonth < 10) month = "0" + birthMonth;
else month = "" + birthMonth;
return birthYear + "/" + month + "/" + birthDay;
}
public String getFirstName() {
return firstName;
}
public String getFamilyName() {
return familyName;
}
public void print() {
System.out.print(firstName + " " + familyName);
System.out.println(" born on " + getBirthDate());
}
}
Create a class called Student
that extends
the Person
class. Add the following instance variables:
What public and private methods?
Write a constructor method, invoke base-class constructor using super
keyword
import java.time.LocalDate;
public class Student extends Person {
private int admissionYear;
public Student(String first, String family,
int admissionYear) {
// invoke base-class constructor
super(first, family);
LocalDate today = LocalDate.now();
if (admissionYear > 1900 && admissionYear <= today.getYear())
this.admissionYear = admissionYear;
}
public int getAdmission() {
return admissionYear;
}
// override annotation
@Override public void print() {
super.print();
System.out.println("Admitted in " + admissionYear);
}
}
poly
means many, morphs
means forms
one interface with multiple implementations
Polymorphism in java:
We will be using Unified Modeling Language (UML) diagrams to understand and plan classes and their inheritance.
+
/-
for public/privateI use the UML class template from Lucidcharts (available through google drive)
Animal.java
public class Animal {
private String species;
private String genus;
private String family;
public Animal (String s, String g, String f) {
species = s;
genus = g;
family = f;
}
public String toString() {
String message;
message = "The " + species;
message += " of genus " + genus;
message += " and family " + family;
return message;
}
}
Cat.java
abstract
superclassAn abstract class cannot be instantiated (an error is thrown)
An abstract class must be subclassed
Methods in an abstract class can also be abstract
– similar to an interface
abstract
example implementationpublic abstract class Animal {
private String species;
private String genus;
private String family;
public Animal (String s, String g, String f) {
species = s;
genus = g;
family = f;
}
public abstract String makesSound();
public String toString() {
String message;
message = "The " + species;
message += " of genus " + genus;
message += " and family " + family;
message += " makes this sound: ";
message += makesSound();
return message;
}
}
protected
keywordThe private
access modifier specifies the variable can only be accessed in its own class
The protected
access modifier specifies that the variable can only be accessed by a subclass of its class
protected
implementationpublic abstract class Animal {
protected String species;
protected String genus;
protected String family;
public Animal () {}
public abstract String makesSound();
public String toString() {
String message;
message = "The " + species;
message += " of genus " + genus;
message += " and family " + family;
message += " makes this sound: ";
message += makesSound();
return message;
}
}
protected
implementationThe classes and inheritance relationships that we worked with so far has been simplified.
With the table mates, improve on the Animal
superclass and its subclasses.
You are to submit your group UML diagram to gradescope for the short assignment 6. Do not submit my UML diagram with annotations, create your own.