Vocab

  • constructor: Where the attributes of a class are defined
  • Overriding: allows a subclass or child class to provide a specific implementation of a method that has already been provided by a super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature, and same return type (or sub-type) as a method in its super-class, then the method in the subclass will override the method in the super-class.
  • Polymorphism: running mutliple things at once with the same name, can be done with different amounts of parameters in method
  • toString method defines and JSONify's data
  • protected is an access modifier so that the attribute isn't affected by outside modifiers.
  • to use constructor of superclass in subclass, need to use super keyword, allowing use of constructors that were made in the superclass

Hack 1

In your own notebook, make any class with 2 attributes and 0 methods. Create a 2 argument constructor for that class. This will be your superclass. Now, create a subclass that extends from the superclass you made. Create 1 additional attribute in your subclass that was not present in the superclass.

Then, create a constructor for the subclass that uses the superclass constructor with the super keyword, and then adds an additional assignment for the third attribute.

public class Animal {
    protected double mass; //protected so the attribute isn't modified outside by outside modifiers
    protected double lifespan;
    public Animal(double mass, double lifespan) { //constructor with parameters of mass and lifespan
        this.mass = mass;
        this.lifespan = lifespan;
    }

    public class Dog extends Animal { //extends superclass into dog subclass
    
        protected String color; 
        
        public Dog(double mass, double lifespan, String color) { //another constructor
            super(mass, lifespan);
            this.color = color;
        }
    }
}

Hack 2

Add a method to the superclass you created before. This method should be very general; it should only have functionality that you know for sure will be needed in almost every single subclass. In your subclass, override this method. Remember that overriding the method will give your subclass the specific functionality it needs from that method.

public class Animal {
    protected double mass;
    protected double lifespan;
    public Animal(double mass, double lifespan) {
        this.mass = mass;
        this.lifespan = lifespan;
    }
    public void sound() {
        System.out.println("sound");
    }

    public class Dog extends Animal {
    
        protected String color; 
        
        public Dog(double mass, double lifespan, String color) {
            super(mass, lifespan);
            this.color = color;
        }
        @Override
        public void sound() { //overrides the sound method, by printing woof instead of sound
            System.out.println("woof");
        }
    }
}
  • polymorphism allows doing a bunch of this in a bunch of ways with inheritance
  • runtime polymorphism is method overriding
  • having two methods with same name but different args and functionalities is method overloading, called static polymorphism
  • early binding is when compiler decides method to be called

Hack 3

Create another subclass from your original superclass. Now, implement method overloading in this subclass. Remember that this means having two methods with the same name, but with different arguments. The method you are using for method overloading doesn't have to exist in the superclass. This will implement Static Polymorphism.

Next, override the method in your superclass in your new subclass. Make sure it has different functionality than your method in the other subclass. This will implement Runtime Polymorphism.

public class Animal {
    protected double mass;
    protected double lifespan;
    public Animal(double mass, double lifespan) {
        this.mass = mass;
        this.lifespan = lifespan;
    }
    public void sound() {
        System.out.println("sound");
    }

    public void die(boolean a) {
        System.out.println("the cat is dead: " + a); //method to be overloaded
    }
    public class Dog extends Animal {
    
        protected String color; 
        
        public Dog(double mass, double lifespan, String color) {
            super(mass, lifespan);
            this.color = color;
        }
        @Override
        public void sound() {
            System.out.println("woof");
        }
    }
    public class Cat extends Animal { //make  another subclass for Cat that extends off of Animal 
    
        protected String breed; 
        
        public Cat(double mass, double lifespan, String breed) {
            super(mass, lifespan);
            this.breed = breed;
        }
        @Override
        public void sound() { //overrides the sound
            System.out.println("meow");
        }

        public void die(boolean a, boolean b) { //method overloading
            System.out.println("the cat is dead: " + a + b);
        }
    }
}

equals Method

  • Compares two strings
  • Returns a boolean value of true if equal, otherwise returns false

toString method example, which prints out the attributes of object

public String dayOfWeekToString() {
    return ("{ \"month\": " + this.month + ", " + "\"day\": " + this.day + ", " + "\"year\": " + this.year + ", "
          + "\"dayOfWeek\": " + this.dayOfWeek + " }");
 }


public String toString() {
    return dayOfWeekToString();
 }