L-9 - Polymorphism and Abstract Classes Flashcards
What is polymorphism in Java?
-Polymorphism is the ability to associate multiple meanings to a single method name using dynamic (late) binding.
What are the three key principles of Object-Oriented-Programming (OOP)?
- Encapsulation - Hiding data using access modifiers.
- Inheritance - Allowing one class to inherit properties from another.
- Polymorphism - Allowing different classes to use the same method names with different behaviors.
What are the benefits of polymorphism?
- Reduces code duplication
- Increases flexibility in using objects of different types
-Allows for late binding (methods are determined at runtime)
What is late binding?
Late binding (dynamic binding) means that the method invoked is determined at runtime, not at compile time.
What is the difference between early binding and late binding?
Early Binding - The method is determined at compile-time
Late binding - The method is determined at runtime (used for overridden methods).
When does Java use late binding?
Java applies late binding to all methods, except:
-private methods
-final methods
-static methods
How does late binding work with inheritance?
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
Do static methods support late binding?
No - static methods are statically bound at compile-time.
Example:
Sale s = new DiscountSale();
s.announcement(); // Calls Sale’s static method, NOT DiscountSale’s
What does ‘final’ do in Java?
‘final’ method - cannot be overridden.
‘final’ class - cannot be inherited.
Why does ‘toString()’ use late binding?
-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()
What is upcasting?
Assigning a derived class object to a base class reference.
Example:
Sale saleVar = new DiscountSale(“Item”, 10, 20); // Upcasting
What is downcasting?
Assigning a base class reference back to a derived class reference.
Example:
DiscountSale dSale = (DiscountSale) saleVar; //Downcasting
incorrect downcasting causes a runtime error.
How can you check if an object is an instance of a class before downcasting?
Use ‘instanceof’:
if (saleVar instanceof DiscountSale) {
DiscountSale d = (DiscountSale) saleVar;
}
What does the ‘clone()’ method do?
It creates a deep copy of an object.
Example:
public Sale clone() {
return new Sale(this);
}
What is the default behavior of ‘clone()’ in Java?
-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
What is an abstract class?
-A class that cannot be instantiated. (Cannot create an object directly. Abstract classes serve as blueprints)
-Contains at least one abstract method.
What is an abstract method?
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();
}
What is an abstract method?
-A method with no body (implementation is deferred to subclasses).
-Must be in an abstract class
Example:
public abstract double getPay(); // No implementation
What is the difference between a concrete class and an abstract class?
-Concrete class : can be instantiated, all methods must be implemented.
-Abstract class : Cannot be instantiated, may contain abstract methods.
What must a subclass do if it inherits from an abstract class?
-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
}
What are the benefits of abstract classes?
- Provide a base template for related classes
-Enforce method implementation in subclasses
-Allow partial method definitions (mix of abstract and concrete methods)
Can you create an object of an abstract class?
No, but you can create references.
Example:
Employee e = new SalariedEmployee(); // ✅ Allowed
Employee e = new Employee(); // ❌ Error
Can you use an abstract class as a type?
Yes, you can declare a variable of an abstract class type.
Example:
Employee emp = new HourlyEmployee();