Inheritance And Polymorphism Flashcards

1
Q

Is a subclass a superset of a superclass?

A

No. It might seem so in the sense that a subclass can have more properties and methods, but the superclass is a superset in regards to its member instances. All children are people, but not all people are children. People is a superclass/superset of children.

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

What keyword do you use to define a subclass?

A
The 'extends' keyword. 
Public class B extends A {
}
In theory, almost all classes are implicitly subclasses of the Object class. Only the Object class itself is not a subclass.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Object-oriented programming allows you to derive new classes from existing classes. This is called ___ ?

A

Inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
What is the output of the following code?
public class Test1 {
  public static void main(String[] args) {
    ChildClass c = new ChildClass();
    c.print();
  }
}
class ParentClass {
  int id = 1;
  void print() {
    System.out.println(id);
  }
}
class ChildClass extends ParentClass {
  int id = 2;
}
A

1

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

Suppose you create a class Square to be a subclass of GeometricObject. Analyze the following code:

class Square extends GeometricObject {
  double length;

Square(double length) {
GeometricObject(length);
}
}

A

The program has a compile error because you attempted to invoke the GeometricObject class’s constructor illegally

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

Analyze the following code:

public class A extends B {
}
class B {
  public B(String s) {
  }
}
A

The program has a compilation error because the default constructor of A invokes the default constructor of B, but B does not have a default constructor.
The program would compile fine if you add the following constructor into A: A(String s) { super(s); }

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

Person pers1 = new Student();

What is the actual type, and what is the declared type?

A

The declared type is Person, and the actual type is Student

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

What is dynamic binding?

A
Dynamic binding means that the actual type of a variable determines which methods are called etc. Consider this example:
Object o = new Student();
o.toString();
Which toString method is being called? Students'? Person's? Object's? The JVM first searches through the Student class. If there's no toString method there, it searches Student's superclass, and continues through superclasses until it finds a toString method. If Student has a toString method, that's the one which is being called.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can you use the ‘super’ keyword to call a superclass constructor?

A

To call a superclass constructor, you must place super() (with optional paramterers) as the first line inside a subclass constructor. If you don’t provide a ‘super’ keyword in the subclass constructor, the compiler automatically puts a hidden super() as the first line in the constructor. If this happens and you don’t have a no-arg constructor in the superclass a compile-error will occur. You can solve this by adding a no-arg constructor in the superclass, or by explicitly calling a superclass constructor with super(parameters) in the subclass constructor.

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

How can you use the ‘super’ keyword to call superclass methods?

A

Consider the common GeometricObject and Circle classes. Circle inherits GeometricObject’s methods. These methods can be accessed by “super.methodName();”, but normally this isn’t neccessary. You can just use “methodName();”. However, consider that both classes has a toString method. Circle’s toString has overriden GeometricObject’s. You could overload the toString method in Circle if you wanted to, and if you wanted to use GeometricObjects toString method here, you would need to do “super.toString()” etc.

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

What does the ‘protected’ keyword do?

A
The 'protected' keyword is a visibility modifier. It allows subclasses (subclasses only) to access data and methods.
There are 4 visibility modifiers: public, protected, default( no modifier), and private.
Private only allows access from the same class, default (no modifier) allows access from classes of same package, protected allows access from subclasses (in any package), and public allows access from any class in any package.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly