Unit 9
Team members: Lily Wu, William Wu, Vidhi Kulkarni, Riya Patil, Saathvika Ajith
public class Cat {
// two attributes
protected boolean hasTail;
protected int numLegs;
// two argument constructor
public Cat(boolean hasTail, int numLegs) {
this.hasTail = hasTail;
this.numLegs = numLegs;
}
}
public class CalicoCat extends Cat {
// calico cat is a type of cat so it can inherit the attributes of Cat
// creates a furType variable which is unique for a calico cat
protected String furType;
public CalicoCat(boolean hasTail, int numLegs, String furType) {
super(hasTail, numLegs);
this.furType = furType;
}
}
public class Cat {
protected boolean hasTail;
protected int numLegs;
public Cat(boolean hasTail, int numLegs) {
this.hasTail = hasTail;
this.numLegs = numLegs;
}
// all cats meow
public void sayMeow() {
System.out.println("meow");
}
}
public class CalicoCat extends Cat {
protected String furType;
public CalicoCat(boolean hasTail, int numLegs, String furType) {
super(hasTail, numLegs);
this.furType = furType;
}
// calico cat says meow differently, method overriding
@Override
public void sayMeow() {
System.out.println("meow meow RAWR");
}
}
public class Cat {
protected boolean hasTail;
protected int numLegs;
public Cat(boolean hasTail, int numLegs) {
this.hasTail = hasTail;
this.numLegs = numLegs;
}
public void sayMeow() {
System.out.println("meow");
}
// cats are cute ;)
public void funFacts() {
System.out.println("cute");
}
}
public class ToygerCat extends Cat {
// method overloading: 2 methods with the same name but different arguments
public void food(String food1) {
System.out.println("Toyger eats " + food1);
}
public void food(String food1, String food2) {
System.out.println("Toyger eats " + food1 + " and " + food2);
}
// method overriding: additional fun fact, overrides the funFacts method in the superclass
@Override
public void funFacts() {
System.out.println("cute and looks like a toy tiger");
}
}
Notes
Inheritance can be used to save what would otherwise be repetitive code. The base class (superclass) can have methods and attributes that are the same with all subclasses. The subclass extends the superclass. Basically, the superclass is the general outline of something (such as a cat), while the subclass is a specific version of the superclass (such as calico cat).
The syntax to extend a superclass is public class subclass extends superclass
.
The constructor of a subclass can also utilize the constructor of the superclass using the super()
keyword, while also having its own extra arguments.
The subclass can access all of the methods in the superclass. However, if you want to override the superclass's method, you can type the same method name in the subclass. The @Override
annotation will indicate that the method is overriding the superclass's method.
Inheritance hiearchies help visualize superclasses and subclasses.
Reference: The object of the class or inherited class
Polymorphism:
Method overriding occurs when the method in the subclass overrides the method in the subclass.
method overloading, occurs when you have muliple methods in a class with the same name. If they have a different number of parameters, a different method will be called depending on the number of arguments you provide.
Other words for method overriding: Runtime polymorphism, dynamic binding, dynamic method dispatch.
Other words for method overloading: Compile time polymorphism, static polymorphism
Early binding: Compiler decides which method to call (method overloading)
Late binding: Method decide at runtime (method overriding)
Object: Superclass of other classes
toString()
: Outputs the attributes of an object and changes a string object to a string
equals()
: Compares two strings