Unit 9 - Inheritance
Notes
Inheritance
A 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. You can have multiple instances of inheritance. Constructors in a sub class needs to have at least the same definition as the old class. When you call super() you call the supper class's constructor which must have all relevant parameters passed into it. @Override needed to state overriding methods of a super class and makes it readable
Creating Reference Using Inheritance Hierarchies
Tree called inheritance hierarchies where lower subclasses inherit upper super classes in the hierarchy
Polymorphism
Allows methods to take on multiple forms for code reusability of methods with different forms/implementations but execute the same way
Super Class
Includes equals() and toString() methods which are the most common methods of the Object class
Vocab
Inheritance & Extends
-
Inheritance: a way for attributes and methods to be inherited from one class to another
-
Extends: allows you to bring those attributes over from one class to another
Subclass Constructor, Super Keyword
- subclass inherits all the members from the superclass
- the constructor of the superclass can be invoked from the subclass
- super keyword: refers to superclass objects
- used to call superclass methods and to access the superclass constructor
// Super Class
public class Animal {
private boolean vegetarian;
private String eats;
private int noOfLegs;
public Animal(){}
public Animal(boolean veg, String food, int legs){
this.vegetarian = veg;
this.eats = food;
this.noOfLegs = legs;
}
public boolean isVegetarian() {
return vegetarian;
}
public void setVegetarian(boolean vegetarian) {
this.vegetarian = vegetarian;
}
public String getEats() {
return eats;
}
public void setEats(String eats) {
this.eats = eats;
}
public int getNoOfLegs() {
return noOfLegs;
}
public void setNoOfLegs(int noOfLegs) {
this.noOfLegs = noOfLegs;
}
}
// SubClass
public class Cat extends Animal{
private String color;
public Cat(boolean veg, String food, int legs) {
super(veg, food, legs);
this.color="White";
}
public Cat(boolean veg, String food, int legs, String color){
super(veg, food, legs);
this.color=color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
class CalculateSquare
{
public void square()
{
System.out.println("No Parameter Method Called");
}
public int square( int number )
{
int square = number * number;
System.out.println("Method with Integer Argument Called:"+square);
}
public float square( float number )
{
float square = number * number;
System.out.println("Method with float Argument Called:"+square);
}
public static void main(String[] args)
{
CalculateSquare obj = new CalculateSquare();
obj.square();
obj.square(5);
obj.square(2.5);
}
}
// A Simple Java program to demonstrate
// Overriding and Access-Modifiers
class Parent {
// private methods are not overridden
private void m1()
{
System.out.println("From parent m1()");
}
protected void m2()
{
System.out.println("From parent m2()");
}
}
class Child extends Parent {
// new m1() method
// unique to Child class
private void m1()
{
System.out.println("From child m1()");
}
// overriding method
// with more accessibility
@Override
public void m2()
{
System.out.println("From child m2()");
}
}
// Driver class
class Main {
public static void main(String[] args)
{
Parent obj1 = new Parent();
obj1.m2();
Parent obj2 = new Child();
obj2.m2();
}
}
abstract class Animal {
public abstract void animalSound();
public void sleep() {
System.out.println("Zzz");
}
}
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Pig extends Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
}
class Dog extends Animal {
public void animalSound() {
System.out.println("The dog says: bow wow");
}
}
class WorldCupTeam {
private String country;
private String jerseyColor;
private int worldCupsWon;
private String mostKnownPlayer;
public WorldCupTeam() {
this.country = "Unknown";
this.jerseyColor = "None";
this.worldCupsWon = 0;
this.mostKnownPlayer = "None";
}
public WorldCupTeam(String country, String jerseyColor, int worldCupsWon, String mostKnownPlayer) {
this.country = country;
this.jerseyColor = jerseyColor;
this.worldCupsWon = worldCupsWon;
this.mostKnownPlayer = mostKnownPlayer;
}
public String getCountry() {
return country;
}
public String getJerseyColor() {
return jerseyColor;
}
public int getWorldCupsWon() {
return worldCupsWon;
}
public String getMostKnownPlayer() {
return mostKnownPlayer;
}
public String toString() {
return country + ": " + "Jersey Color = " + jerseyColor +
", World Cups Won = " + worldCupsWon +
", Most Known Player = " + mostKnownPlayer;
}
}
class Argentina extends WorldCupTeam {
private int watchPartiesInBuenosAires;
public Argentina(int worldCupsWon, String mostKnownPlayer, int watchPartiesInBuenosAires) {
super("Argentina", "Blue & White", worldCupsWon, mostKnownPlayer);
this.watchPartiesInBuenosAires = watchPartiesInBuenosAires;
}
public String toString() {
String parentString = super.toString();
return parentString + ", Watch Parties in Buenos Aires = " + watchPartiesInBuenosAires;
}
}
class Brazil extends WorldCupTeam {
public Brazil() {
super("Brazil", "Yellow & Gold", 5, "Neymar");
}
}
class England extends WorldCupTeam {
public England() {
super("England", "Blue Void", 1, "Phil Foden");
}
}
class France extends WorldCupTeam {
public France() {
super("France", "Blue", 2, "Kylian Mbappe");
}
}
class Portugal extends WorldCupTeam {
public Portugal() {
super("Portugal", "Red & Green", 0, "Cristiano Ronaldo");
}
}
class Main {
public static void main(String[] args) {
WorldCupTeam generalTeam = new WorldCupTeam();
System.out.println(generalTeam);
Argentina argentina = new Argentina(3, "Lionel Messi", 43);
System.out.println(argentina);
Brazil brazil = new Brazil();
System.out.println(brazil);
England england = new England();
System.out.println(england);
France france = new France();
System.out.println(france);
Portugal portugal = new Portugal();
System.out.println(portugal);
}
}
Main.main(null)
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 String birthday;
public Person (String name, String birthday){
this.name = name;
this.birthday = birthday;
}
public String getName(){
return name;
}
public int getAge(){
if (this.birthday != null) {
// LocalDate birthDay = this.birthday.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
// return Period.between(birthDay, LocalDate.now()).getYears();
// too lazy to use a name thing
}
return -1;
}
}
public class Student extends Person {
private int grade;
private double gpa;
public Student (String name, String birthday, int grade, double gpa) {
super(name, birthday);
this.grade = grade;
this.gpa = gpa;
}
public String getName() {
return name;
}
public String getBirthday() {
return birthday;
}
public int getGrade(){
return grade;
}
@Override
public String toString(){
return name + ": Birthday: " + birthday + ", Grade: " + grade + ", GPA: " + gpa;
}
}
public class Teacher extends Person {
private String subject;
public Teacher (String name, String birthday, String subject){
super(name, birthday);
this.subject = subject;
}
@Override
public String toString(){
return name + ": Birthday: " + birthday + ", Subject: " + subject;
}
}
class Bob extends Student {
public Bob() {
super("Bob", "January 1", 12, 4.0);
}
}
class Ben extends Student {
public Ben() {
super("Ben", "March 17th, 2006", 11, 4.0);
}
}
class MrMort extends Teacher {
public MrMort() {
super("John Mortensen", "January 1st, 1900", "Computer Science");
}
}
public class Main{
public static void main(String[] args){
Bob bob = new Bob();
System.out.println(bob);
Ben ben = new Ben();
System.out.println(ben);
MrMort mort = new MrMort();
System.out.println(mort);
}
}
Main.main(null);