Week 3 Flashcards

1
Q

What is inheritance

A

The process where one class aquires the attributes and methods of another class. The classes are divided into superclass and subclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does one make a class inherite attributes and methods from another class?

A

when creating the class one must add the keyword extends

public class Bike extends Vehicle{}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does it mean to override a method?

A

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()”);
—-}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does it mean to overload a method?

A

Make a new version of an exiting method, but with inputs, different inputs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is an interface?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does one create an interface?

A

public interface Name{}

public interface Prey{

—-void flee(); // body is empty

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does a class use an interface?

A

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”);
—-}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is upcasting?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is downcasting?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are exceptions?

A

Exceptions are events that occures during the execution of a program that disrupts the normal flow of instructions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How should one handel exceptions?

A

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();
——–}

—-}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly