Oop Inhertiance Java Midterm Notes
Oop Inhertiance Java Midterm Notes
In simple words, Inheritance means 'one class getting features from another class'.
Syntax of Inheritance
class ParentClass {
Example of Inheritance
class Animal {
void sound() {
void bark() {
System.out.println("Dog barks");
}
Note: Java does not support multiple inheritance with classes to avoid ambiguity.
A base class BankAccount will hold general account details. A derived class
SavingsAccount will extend it and include interest calculation.
Java Code
class BankAccount {
String accountNumber;
double balance;
this.accountNumber = accountNumber;
this.balance = balance;
void displayBalance() {
double interestRate;
super(accountNumber, balance);
this.interestRate = interestRate;
double calculateInterest() {
return (this.balance * this.interestRate) / 100;
void displaySavingsDetails() {
displayBalance();
myAccount.displaySavingsDetails();
Output
Account Number: ACC12345