Lecture 11 and 12- Inheritance, Method overriding, Constructor, Object class Flashcards

1
Q

What does inheritance stand for in OOP?

A

In OO languages, classes can be derived from other classes, thereby inheriting fields and methods from those classes.

Java only supports single inheritance - each subclass can have one and only one direct super-class.

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

explain super class and subclass ?

A
Superclass or base, parent class
Subclass or derived, child, extended class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the benefit of inheritance in OOP?

A

Code re-usability;

Better program structure.

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

What are the 4 areccess modifiers?

A

private
default
protected
public

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

Who can inherit by a private superclass?

A

any subclass whithin that superclass

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

What subclass can inherit from a default superclass?

A

Any subclass is in the same package as its parent, it also inherits the default (or package-private) attributes and methods of the parent.

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

When can a subclass inherit from a public or protected superclass?

A
A subclass inherits all of the public and protected attributes and methods of its parent as well as all its
ancestors, no matter what package the subclass is in.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What can a subclass do apart from inheriting ?

A

add its own attributes and methods.
add same-signature methods - method overriding.
add same-name attributes (but not recommended) -
attribute hiding .

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

What is method overriding ?

A

A subclass defines a method which has the same name, number and type of parameters, and return type as a method in the superclass.

Recall Method overloading: If a class has multiple methods having the same name but different in parameters.

When overriding a method, it is recommended to use the @Override annotation, though it is optional.

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

What is a must for an overriding method ?

A

The access modifier for an overriding method can allow more, but not less, access than the overridden method.

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

Are constructors inherited ?

A

Constructors are not inherited. Each class defines its own constructors.

But the constructor of the super-class can be invoked from the subclass, using super().

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

When can the final keyword be used ?

A

A final attribute: it is a constant.

final int MAX BORROW BOOKS = 5;

A final method: we can not override this method.

final void eat(){…}

A final class: we can not extend it.

public final class String {...}//String class
doesn’t have any subclasses
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is finally used for?

A

try-catch-finally

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

What is upcasting ?

A
Upcasting: assign a subclass instance to a superclass
reference.

Animal d=new Dog(“black”,5,10); //upcasting

You can invoke all the methods defined in the Animal class

d. eat();//the superclass’s method
d. sleep();//the subclass’s version is invoked.

You CANNOT invoke methods defined in the Dog class for the reference d.

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

What is downcasting ?

A
Downcasting: revert a substituted instance back to a
subclass reference.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is commonly done to avoid class cast exception?

A

To avoid this ClassCastException, the common choice is to check the object’s class before downcasting by usin the instanceof operator.

if (d instanceof Dog) {
Dog d2 = (Dog)d;//downcasting safely
}

17
Q

What is the instanceof operator ?

A

instanceof is an operator to verify if an object is an instance of a particular class.

18
Q

What is the instanceof operator ?

A

instanceof is an operator to verify if an object is an instance of a particular class.

Dog d1 = new Dog("black",5,10);
System.out.println(d1 instanceof Dog); // true
System.out.println(d1 instanceof Animal); // true
19
Q

What does binding refer to ?

A

Binding refers to the linking of a method call to its body (of an object type).

20
Q

What are the two types of binding ? Explain them

A

Static/early binding:
When type of the object is determined at compile time(by the compiler).

Dynamic/late binding:
When type of the object is determined at runtime.

21
Q

What is late binding?

A

When compiler is not able to resolve the call/binding at compile time, such binding is known as dynamic binding or late binding.

Overriden methods
Animal d=new Dog(“black”,5,10); //upcasting
d.sleep();//late binding

Or for example when compiling the Routine class, the compiler does not know which sleep() method will be called.

public class Routine {
\_\_\_\_public static void aMethod(Animal a){
\_\_\_\_\_\_\_\_a.sleep();
\_\_\_\_}
// ... other methods and attributes
}
22
Q

When does static binding happen ?

A

Methods cannot be overriden:
static methods
private methods
final methods

Overloaded methods
void zoom(double factor){...}
void zoom(double factorX, double factorY){...}
23
Q

What is Polymorphism ?

A

Polymorphism means many forms in greek.
In OOP it is the mechanism of performing a single action in different ways.

In OOP languages, the polymorphism is implemented through method overriding (or late binding), i.e., the superclass defines a common method, its subclasses give different implementations by overriding it.

24
Q

What is an abstract method ?

A

An abstract method: a method without an implementation.

abstract void getArea();

25
Q

What is an abstract class?

A

An abstract class: a class that is declared abstract - it may or may not include abstract methods.

abstract class Shape{…}

26
Q

Characteristics of abstract classes:

A

If one class has an abstract method, it must be declared as abstract.

Abstract classes cannot be instantiated.
Animal a=new Animal("red",10);//compile error

If one class extends a class that has abstract methods, it must either provide the implementation of all abstract methods or be declared abstract as well.

27
Q

What is abstraction ?

A

a concept of hiding all but the relevant data in order to reduce complexity and increase efficiency.

28
Q

cases of abstraction?

A

Encapsulation is a kind of abstraction.
Abstract classes - achieve partial abstraction
Interfaces - achieve fully abstraction (100%)