L-9 - Polymorphism and Abstract Classes Flashcards

1
Q

What is polymorphism in Java?

A

-Polymorphism is the ability to associate multiple meanings to a single method name using dynamic (late) binding.

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

What are the three key principles of Object-Oriented-Programming (OOP)?

A
  1. Encapsulation - Hiding data using access modifiers.
  2. Inheritance - Allowing one class to inherit properties from another.
  3. Polymorphism - Allowing different classes to use the same method names with different behaviors.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the benefits of polymorphism?

A
  • Reduces code duplication
  • Increases flexibility in using objects of different types

-Allows for late binding (methods are determined at runtime)

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

What is late binding?

A

Late binding (dynamic binding) means that the method invoked is determined at runtime, not at compile time.

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

What is the difference between early binding and late binding?

A

Early Binding - The method is determined at compile-time

Late binding - The method is determined at runtime (used for overridden methods).

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

When does Java use late binding?

A

Java applies late binding to all methods, except:
-private methods
-final methods
-static methods

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

How does late binding work with inheritance?

A

If a derived class overrides a method, the overridden version is used at runtime, even if a base class reference is used.

Sale s = new DiscountSale(“Item”, 10, 20); // Base class reference
System.out.println(s.bill()); // Calls DiscountSale’s bill() method

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

Do static methods support late binding?

A

No - static methods are statically bound at compile-time.

Example:

Sale s = new DiscountSale();
s.announcement(); // Calls Sale’s static method, NOT DiscountSale’s

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

What does ‘final’ do in Java?

A

‘final’ method - cannot be overridden.

‘final’ class - cannot be inherited.

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

Why does ‘toString()’ use late binding?

A

-Java implicitly calls ‘toString()’ when printing an object

-The overridden version in the subclass is used.

Example:
Sale s = new DiscountSale(“Item”, 10, 20);
System.out.println(s); // Calls DiscountSale’s toString()

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

What is upcasting?

A

Assigning a derived class object to a base class reference.
Example:

Sale saleVar = new DiscountSale(“Item”, 10, 20); // Upcasting

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

What is downcasting?

A

Assigning a base class reference back to a derived class reference.

Example:

DiscountSale dSale = (DiscountSale) saleVar; //Downcasting

incorrect downcasting causes a runtime error.

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

How can you check if an object is an instance of a class before downcasting?

A

Use ‘instanceof’:

if (saleVar instanceof DiscountSale) {
DiscountSale d = (DiscountSale) saleVar;
}

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

What does the ‘clone()’ method do?

A

It creates a deep copy of an object.
Example:

public Sale clone() {
return new Sale(this);
}

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

What is the default behavior of ‘clone()’ in Java?

A

-It is defined in the ‘Object’ class.
-Must be overridden to provide deep copies.
-Before Java 5.0, ‘clone()’ had to return ‘Object’, requiring a cast.

Sale copy = (Sale) original.clone(); // Required before Java 5.0

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

What is an abstract class?

A

-A class that cannot be instantiated. (Cannot create an object directly. Abstract classes serve as blueprints)
-Contains at least one abstract method.

17
Q

What is an abstract method?

A

A method that has no implementation int he class where it is declared. It must be implemented by subclasses.

public abstract class Employee {
public abstract double getPay();
}

18
Q

What is an abstract method?

A

-A method with no body (implementation is deferred to subclasses).

-Must be in an abstract class

Example:
public abstract double getPay(); // No implementation

19
Q

What is the difference between a concrete class and an abstract class?

A

-Concrete class : can be instantiated, all methods must be implemented.

-Abstract class : Cannot be instantiated, may contain abstract methods.

20
Q

What must a subclass do if it inherits from an abstract class?

A

-Implement all abstract methods.

  • If it does not implement all, it must also be declared abstract.

Example:
public class SalariedEmployee extends Employee {
@Override
public double getPay() { return salary; } // Must be implemented
}

21
Q

What are the benefits of abstract classes?

A
  • Provide a base template for related classes

-Enforce method implementation in subclasses

-Allow partial method definitions (mix of abstract and concrete methods)

22
Q

Can you create an object of an abstract class?

A

No, but you can create references.
Example:

Employee e = new SalariedEmployee(); // ✅ Allowed
Employee e = new Employee(); // ❌ Error

23
Q

Can you use an abstract class as a type?

A

Yes, you can declare a variable of an abstract class type.

Example:
Employee emp = new HourlyEmployee();