Inheritance Lesson Hacks
Notes of Key Concepts
- Each class can extend from it's base class
- When a class extends from its base class, it inherits the methods and other attributes
- Protected: an access modifier so that the attribute isn't affected by access modifier that isn't affected by outside modifiers
- If the attributes of the subclass are exactly the same as the attributes of the superclass, then we can just use the constructor of the superclass.
- An additional attribute that's added to the subclass class does not have to be added to the superclass if it does not apply to it
- Overriding methods allows the subclass to provide specific implementation of a method
- Has to be already provided by the super-class
- If a method in a subclass has the same name, same parameters or signature, and same return type as a method in its super-class, then the method in the subclass will override the method in the super-class.
-
Uses of the super keyword
- Use constructors in the superclass
- Use the methods in the superclass in the child class
- You can use a inheritance hierarchy to organize references
-
Polymorphism: You can do one action many ways
- Runtime Polymorphism: method overriding
- Compile Time Polymorphism: also using method overrloading
- Object class is the superclass of all other classes in Java
- toString Method: Prints out the attributes of an object and converts a string object to string
- equals Method: Compares two strings and returns a boolean value of true if equal
public class Candy {
protected double candyCalories;
protected double candyRating;
protected double candySize;
public Candy(double candyCalories, double candySize, double candyRating){
this.candyCalories = candyCalories;
this.candyRating = candyRating;
this.candySize = candySize;
}
public void healthWarning() {
System.out.println("Candy is not healthy. Consume in moderation!");
}
public static void main(String[] args){
Candy can = new Candy(100, 20, 9.4);
can.healthWarning();
}
}
Candy.main(null);
public class Starbursts extends Candy{
protected String candyColor;
public Starbursts(double candyCalories, double candySize, double candyRating, String candyColor){
super(candyCalories, candySize, candyRating);
this.candyColor = candyColor;
}
@Override
public void healthWarning() {
System.out.println("Starbursts are not good for you!!!");
}
public void type(String type1){
System.out.println("Your candy is " + type1 + ".");
}
public void type(String type1, String type2) {
System.out.println("Your candy is " + type1 + " and " + type2 + ".");
}
public static void main(String[] args){
Starbursts star = new Starbursts(20, 0.5, 6.5, "Red");
star.healthWarning();
star.type("sweet");
star.type("sweet", "chewy");
}
}
Starbursts.main(null);
public class Smarties extends Candy{
protected String candyColor;
public Smarties(double candyCalories, double candySize, double candyRating, String candyColor){
super(candyCalories, candySize, candyRating);
this.candyColor = candyColor;
}
@Override
public void healthWarning() {
System.out.println("Smarties are not good for you!!!");
}
public static void main(String[] args){
Smarties smart = new Smarties(20, 0.5, 6.5, "Red");
smart.healthWarning();
}
}
Smarties.main(null);