Chapter 2: Object Orientation Flashcards

1
Q

What is encapsulation?

A

Hiding implementation details, and only expose instance variables through public methods.

  • protect instance variables
  • make public accessor methods (getters/setters)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is polymorphism?

A

You can treat any subclass as the superclass (or a interface). Every type in java is polymorphic because they can be treated as their own type as well as the Object type.

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

Describe a IS-A relationship

A

In java expressed using the extends and implements keyword (inheritance).

Car IS-A vechicle
Horse IS-A animal

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

Describe a HAS-A relationship

A

Whether a object has a reference to another object.

Car HAS-A engine

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

What is wrong?

class PlayerPiece extends GameShape, Animatable {
}
A

A class can only extend a single class.

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

What are the 5 important rules for overriding methods?

A
  • Argument list must exactly match
  • Return type must be the same, or a subtype.
  • The access level can be less restrictive
  • Can only override non-final protected or public methods (or default if in the same package).
  • You can’t introduce new exceptions in the throw clause, you can throw fewer exceptions.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
Which overloaded method will be called?
class Foo {
   public void doStuff(Animal a) {} // 1
   public void doStuff(Horse a) {} // 2
}
Animal animal = new Horse();
new Foo().doStuff(animal);
A

doStuff(Animal a) will be called.

The reference type (not the object type) is used to determine which method is invoked.

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

What is overloading?

A

Adding a new method with the same name as an already existing method but with an different argument list

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

What is a downcast?

A

Casting a object down to a more specific type in the inheritance tree.

Dog d = (Dog) animal;

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

What is a upcast?

A

Casting a object to a more generic type in the inheritance tree.

Animal a = (Animal) dog;
Animal a = dog; // implicit cast

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
What is wrong?
interface Pet { void foo(); }
class Dog implements Pet { void foo() {} }
class Beagle extends Dog implements Pet {}
A

Nothing. A subclass definining a interface that is already implemented by a superclass is perfectly fine.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
True or False:
A class can extend multiple classes
A

False

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

True or False:

A interface can extend multiple classes

A

False

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

True or False:

A interface can extend multiple interfaces

A

True

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

Finish the following senteces about return types:

  1. A overloaded method return type ….
  2. A overriden method return type ….
A
  1. Can be completely different from the original return type.
  2. Must be the same or a subclass of the original return type.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What can be said about the compiler provided default constructor?

A
  • Only inserted if no constructors are defined in the class.
  • It is always a no-arg constructor
  • Has the same visibility as the class
  • If the super-class doesnt have a no-arg constructor the the default-constructor will not be supplied.
17
Q
Which static method is called?
class Animal {
public static void doStuff() {} // 1
}
class Dog extends Animal {
public static void doStuff() {} // 2
}
Animal a = new Dog();
a.doStuff();
A

1

For static method invocation the reference variable type is used. And not the actual object type.

18
Q

What is losely coupled and tightly coupled?

A

Losely: If the only knowledge A has about B, is what B has exposed through its interface.
Tightly: If A relies on parts of B that are not exposed through its interfase

19
Q

What is High Cohesion?

A

Cohesion is used to indicate the degree of which a class has a single, well-focused purpose. The more focused a class is, the higher the cohesion.

20
Q
What will be printed?
class Sub
{
    Sub(){ startUp("StartUp"); }
    void startUp(String s){ System.out.println("InSub"); }
}
class SubSub extends Sub
{
    SubSub(){  }
    void startUp(String s){  System.out.println("InSubSub");   }
}
public class TestClass
{
    public static void main(String args[]){ new SubSub(); }
}
A

InSubSub

21
Q

Consider these two interfaces:

interface I1
{
   void m1() throws java.io.IOException;
}
interface I2
{
   void m1() throws java.sql.SQLException;
}

What methods have to be implemented by a class that says it implements I1 and I2 ?

A
void m1() {}
void m1() throws java.sql.SQLException {}
void m1() throws java.io.IOException {}

Are all valid, a implementation class may always choose to take less risk (throw less exceptions).

22
Q
What will be the output?
class A { int i = 10;  int m1( ) { return i; } }
class B extends A { int i = 20;  int m1() { return i; } }
class C extends B { int i = 30;  int m1() { return i; } }

System.out.println(o1.m1( ) );
System.out.println(o2.i );

A

30, 20

Variables are SHADOWED and methods are OVERRIDDEN.
Which variable will be used depends on the class that the variable is declared of.
Which method will be used depends on the actual class of the object that is referenced by the variable.