Inheritance and Polymorphism Flashcards
class reusability has two forms …. and ….
composition and inheritance
composition is called a ….
has- a
has - a or composition means
placing a reference / object of a class in another class
inheritance is (is-a)
extending a class with another class
inheritance means
that a new class inherits from an existing class, it inherits all its members and characteristics
subclass constructor has to call… directly or indirectly
base/parent class
base/ parent class is called using
super()
super can be the last statement in the constructor of subclass (yes / no)
no, it must be the first statement in the subclass
why does super class constructor must be called
to make sure all class members are initialized
access modifier for an overriding method has to be the same ?
no it can be more but not less ( more access is given )
if an access modifier protected then the child class can be ?
public or none
any method defined in java is overridable by default unless it is ….
final
method of subclass has the same signature of super class?
true
method signture is
same name, same parameters and return type
overloading is between methods
between methods of the same class
same method name but different number of parameters or parameter type but not return type is considered overloading
true
a class can have how many parent classes?
exactly one
abstracted can not be …. or…..
inherited or instantiated
a method declared abstract has no …
body
abstract has to be
overridden
abstract class can contain … classes
both concrete (non-abstract) and abstract
constructors and static methods cant be declared abstract ? why?
yes , because they cant be inherited
we should call super for them to get invoked
can an abstract class have a constructor ?
yes
what happens if we try calling an abstract class constructor ?
compilation error
if there at least one abstract method the class
the class must be abstract
an abstract can be instantiated with a reference to any of its subclasses which is called an
upcasting parent name = new sub()
polymorphism is
using methods to perform different tasks this allows us to perform a single action in different ways
forms of polymorphism
overloading and overriding
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
dynamic binding
is making a thing change or overloaded
dynamic binding happens in
runtime
in dynamic binding the reference of an object is used? t or f
false
dynamic binding speed is
low
example for dynamic binding is
method overriding
static binding happens in
compile time
in static binding the … of an object is used
reference
speed of static binding is
high
static binding example is
overloading
upcasting is
converting an object of a subclass to its superclass done implicitly (indirectly)
downcasting is
converting an object of a superclass to one of its subclasses must be explicitly (directly)
Upcasting example
Person p; Employee emp = new Employee(); p = emp;
DownCasting example
Person p = new Employee();
Employee emp;
emp = (Employee) p;
instanceOf operator can be used to …
to check whether an object is a certain class type or not
can you assign a superclass variable to superclass?
true
can you assign a subclass variable to subclass object?
true
can you assign superclass variable to a subclass object ?
true
can you assign a subclass variable superclass object?
nope, must be done via explicit casting