Inheritance and Polymorphism Flashcards

1
Q

class reusability has two forms …. and ….

A

composition and inheritance

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

composition is called a ….

A

has- a

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

has - a or composition means

A

placing a reference / object of a class in another class

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

inheritance is (is-a)

A

extending a class with another class

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

inheritance means

A

that a new class inherits from an existing class, it inherits all its members and characteristics

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

subclass constructor has to call… directly or indirectly

A

base/parent class

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

base/ parent class is called using

A

super()

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

super can be the last statement in the constructor of subclass (yes / no)

A

no, it must be the first statement in the subclass

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

why does super class constructor must be called

A

to make sure all class members are initialized

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

access modifier for an overriding method has to be the same ?

A

no it can be more but not less ( more access is given )

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

if an access modifier protected then the child class can be ?

A

public or none

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

any method defined in java is overridable by default unless it is ….

A

final

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

method of subclass has the same signature of super class?

A

true

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

method signture is

A

same name, same parameters and return type

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

overloading is between methods

A

between methods of the same class

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

same method name but different number of parameters or parameter type but not return type is considered overloading

A

true

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

a class can have how many parent classes?

A

exactly one

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

abstracted can not be …. or…..

A

inherited or instantiated

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

a method declared abstract has no …

20
Q

abstract has to be

A

overridden

21
Q

abstract class can contain … classes

A

both concrete (non-abstract) and abstract

22
Q

constructors and static methods cant be declared abstract ? why?

A

yes , because they cant be inherited

we should call super for them to get invoked

23
Q

can an abstract class have a constructor ?

24
Q

what happens if we try calling an abstract class constructor ?

A

compilation error

25
if there at least one abstract method the class
the class must be abstract
26
an abstract can be instantiated with a reference to any of its subclasses which is called an
``` upcasting parent name = new sub() ```
27
polymorphism is
using methods to perform different tasks this allows us to perform a single action in different ways
28
forms of polymorphism
overloading and overriding
29
why polymorphism?
allows us to perform a single action in different ways | write general methods in super and implements them later in detail in subclasses
30
dynamic binding
is making a thing change or overloaded
31
dynamic binding happens in
runtime
32
in dynamic binding the reference of an object is used? t or f
false
33
dynamic binding speed is
low
34
example for dynamic binding is
method overriding
35
static binding happens in
compile time
36
in static binding the ... of an object is used
reference
37
speed of static binding is
high
38
static binding example is
overloading
39
upcasting is
converting an object of a subclass to its superclass done implicitly (indirectly)
40
downcasting is
converting an object of a superclass to one of its subclasses must be explicitly (directly)
41
Upcasting example
``` Person p; Employee emp = new Employee(); p = emp; ```
42
DownCasting example
Person p = new Employee(); Employee emp; emp = (Employee) p;
43
instanceOf operator can be used to ...
to check whether an object is a certain class type or not
44
can you assign a superclass variable to superclass?
true
45
can you assign a subclass variable to subclass object?
true
46
can you assign superclass variable to a subclass object ?
true
47
can you assign a subclass variable superclass object?
nope, must be done via explicit casting