Week 3 Flashcards
What is inheritance
The process where one class aquires the attributes and methods of another class. The classes are divided into superclass and subclass.
How does one make a class inherite attributes and methods from another class?
when creating the class one must add the keyword extends
public class Bike extends Vehicle{}
What does it mean to override a method?
Changing methods inherited from a superclass to suit the subclass better
class Child extends Parent {
—-// This method overrides show() of Parent
—-@Override
—-void show() {
——–System.out.println(“Child’s show()”);
—-}
}
What does it mean to overload a method?
Make a new version of an exiting method, but with inputs, different inputs.
What is an interface?
An interfaces is a tempalte that can be applied to a class. It specifies what a class has/must do. Methods in an interface has no body. A class can have mutiple interfaces.
How does one create an interface?
public interface Name{}
public interface Prey{
—-void flee(); // body is empty
}
How does a class use an interface?
The class must use the keyword implements
// The interface in another file
public interface Prey{
—-void fleet(); // body is empty
}
// the class using the interface
public class Rabbit implemets Prey {
—-public void flee(){
——–System.out.println(“Rabbit is fleeing”);
—-}
}
What is upcasting?
Taking a object, like Dog, and casting it as a superclass type, like Animal. This is done implicitly, no problems.
Animal myAnimal = new Dog() //create a dog,, but casting it as animal.
What is downcasting?
Taking a superclass type, like Animal, and casting it as a subclass type, like Dog. Not implicit. When using a downcast object it is advice to use an if-else statement with the keword instanceof
if(Animal instanceof Dog){}
Avoids exceptions where Animal is not Dog.
Dog myDog = (Dog)animal; // creating a animal, but casting it as Dog.
What are exceptions?
Exceptions are events that occures during the execution of a program that disrupts the normal flow of instructions.
How should one handel exceptions?
One should use try block and catch block. try block is wraped around dangerous coude, and followed by a catch block, that specifies the exceptions and have instructions for what to do when the exceptions are caught. This will make sure that the flow of the program is not interupted.
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
----public static void main(String[] args) { --------// exception = an event that occurs during the execution of a program that, --------// disrupts the normal flow of instructions
——–Scanner scanner = new Scanner(System.in);
——–try {
———–System.out.println(“Enter a whole number to divide: “);
———–int x = scanner.nextInt();
———–System.out.println(“Enter a whole number to divide by: “);
———–int y = scanner.nextInt();
———–int z = x/y;
————System.out.println(“result: “ + z);
——–}
——–catch(ArithmeticException e) {
————System.out.println(“You can’t divide by zero! IDIOT!”);
——–}
——–catch(InputMismatchException e) {
————System.out.println(“PLEASE ENTER A NUMBER OMFG!!!”);
——–}
——–catch(Exception e) {
————System.out.println(“Something went wrong”);
——–}
——–finally {
————scanner.close();
——–}
—-}
}