Flashcard 1
Q: What is inheritance in Java?
A: Inheritance is the process by which a child (subclass) automatically includes public or protected
primitives, objects, or methods defined in the parent class, allowing for code reuse and method
overriding.
Flashcard 2
Q: How do you declare inheritance in Java?
A: Use the extends keyword to inherit properties of a class.
java
Copy code
class Dog extends Animal {
// Dog inherits properties of Animal
Flashcard 3
Q: What is method overriding?
A: When a subclass provides a specific implementation of a method already defined in its superclass.
java
Copy code
@Override
public void sound() {
System.out.println("Bark");
Flashcard 4
Q: What is the purpose of the super keyword?
A: The super keyword differentiates superclass members from subclass members if they have the
same names, and invokes the superclass constructor from the subclass.
Flashcard 5
Q: Provide an example of using the super keyword.
java
Copy code
class Dog extends Animal {
Dog() {
super(); // calls Animal's constructor
Flashcard 6
Q: What are the types of inheritance in Java?
A: - Single Inheritance
• Multilevel Inheritance
• Hierarchical Inheritance
Java does not support multiple inheritance.
Flashcard 7
Q: How does encapsulation work in Java?
A: Encapsulation involves wrapping code and data into a single unit, making class variables private
and accessing them via public getter and setter methods.
Flashcard 8
Q: Example of encapsulation in Java.
java
Copy code
public class BankAccount {
private double balance;
public double getBalance() {
return balance;
public void deposit(double amount) {
if (amount > 0) balance += amount;
}
Flashcard 9
Q: Why use getter and setter methods?
A: They protect data by controlling how variables are accessed and modified, allowing internal
implementation changes without affecting external code.
Flashcard 10
Q: Define the BankAccount withdraw method with validation checks.
java
Copy code
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient funds or invalid amount.");