Program1:Inheritance
//Base class
class Vehicle {
void start() {
System.out.println("The vehicle is starting.");
}
}
//Derived class
class Car extends Vehicle {
void honk() {
System.out.println("The car is honking.");
}
}
//Main class to test the inheritance
public class inherit3 {
public static void main(String[] args) {
Car myCar = new Car();
myCar.start(); // Inherited method from Vehicle
myCar.honk(); // Method from Car
}
}
Output:
The vehicle is starting.
The car is honking.
Program2: :Inheritance
//Base class
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
//Derived class
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
//Main class to test the inheritance
public class inherit2 {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Inherited method from Animal
myDog.bark(); // Method from Dog
}
}
Output:
This animal eats food.
The dog barks.
Program 3: Inheritance with constructor
//Base class
class Person {
Person() {
System.out.println("Person constructor called.");
}
}
//Derived class
class Student extends Person {
Student() {
System.out.println("Student constructor called.");
}
}
//Main class to test the inheritance
public class inherit4 {
public static void main(String[] args) {
Student myStudent = new Student();
}
}
Program 4:Inheritance
class Bank{
double GetInterestRate(){
return 0.0;
}
}
class SBI extends Bank{
double GetInterestRate(){
return 5.5;
}
}
class ICIC extends Bank{
double GetInterestRate(){
return 6.0;
}
}
public class ij{
public static void main(String[] args){
SBI s= new SBI();
System.out.println("rate of interest of sbi is :"
+s.GetInterestRate());
ICIC i= new ICIC ();
System.out.println("rate of interest of icic is :" +
i.GetInterestRate());
Bank b= new SBI();
System.out.println("rate of interest of sbi is :" +
b.GetInterestRate());
}
}
Output:
rate of interest of sbi is :5.5
rate of interest of icic is :6.0
rate of interest of sbi is :5.5
Program 4:Private members
class Car {
// Private member variable
private String model;
// Getter method for 'model'
public String getModel() {
return model;
}
// Setter method for 'model'
public void setModel(String model) {
this.model = model;
}
}
public class Main {
public static void main(String[] args) {
// Creating an instance of Car
Car myCar = new Car();
// Setting the private member using the setter
myCar.setModel("Tesla Model S");
// Accessing the private member using the getter
System.out.println("Car model: " + myCar.getModel());
}
}
Program 5:
public class privateerror {
public static void main(String[] args) {
B b =new B();
b.sum();
}
class A {
int i; // public by default
private int j; // private to A
void setij(int x, int y) {
i = x; j = y;
}
}
// A's j is not accessible here.
class B extends A {
int total; void sum() {
total = i + j; // ERROR, j is not accessible here
}
}
Program 6:
//BankAccount.java
class BankAccount {
protected double balance;
public BankAccount(double initialBalance) {
if (initialBalance >= 0) {
this.balance = initialBalance;
} else {
this.balance = 0;
System.out.println("Initial balance can't be negative. Set to 0.");
}
}
// Method to deposit money
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposit successful. New balance: $" + balance);
} else {
System.out.println("Deposit amount must be positive.");
}
}
// Method to withdraw money
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrawal successful. New balance: $" +
balance);
} else {
System.out.println("Invalid withdrawal amount. Current balance: $" +
balance);
}
}
// Method to check the balance
public double getBalance() {
return balance;
}
}
//SavingsAccount.java
class SavingsAccount extends BankAccount {
public SavingsAccount(double initialBalance) {
super(initialBalance);
}
// Override the withdraw method to prevent balance from going below 100
@Override
public void withdraw(double amount) {
if (amount > 0 && (balance - amount) >= 100) {
balance -= amount;
System.out.println("Withdrawal successful. New balance: $" +
balance);
} else {
System.out.println("Withdrawal failed. Minimum balance of $100
required. Current balance: $" + balance);
}
}
}
//Main.java
public class Main {
public static void main(String[] args) {
// Create a regular bank account
BankAccount myAccount = new BankAccount(500);
myAccount.deposit(100);
myAccount.withdraw(200);
myAccount.withdraw(600); // Invalid withdrawal
// Create a savings account
SavingsAccount mySavings = new SavingsAccount(500);
mySavings.deposit(150);
mySavings.withdraw(400); // Should succeed
mySavings.withdraw(200); // Should fail due to minimum balance
}
}
Program 7: Multilevel
//Base class (Grandparent)
class Animal {
public void eat() {
System.out.println("This animal eats food.");
}
}
//Derived class (Parent) that inherits from Animal
class Mammal extends Animal {
public void breathe() {
System.out.println("This mammal breathes air.");
}
}
//Derived class (Child) that inherits from Mammal
class Dog extends Mammal {
public void bark() {
System.out.println("The dog barks.");
}
}
public class Multilevel {
public static void main(String[] args) {
// Creating an instance of Dog
Dog myDog = new Dog();
// Calling methods from all levels of the hierarchy
myDog.eat(); // From Animal class
myDog.breathe(); // From Mammal class
myDog.bark(); // From Dog class
}
}
Program 8: Hierarchical
/// Parent class
class Vehicle {
public void start() {
System.out.println("Vehicle is starting.");
}
}
// Child class 1
class Cars extends Vehicle {
public void drive() {
System.out.println("Car is driving.");
}
}
// Child class 2
class Bike extends Vehicle {
public void ride() {
System.out.println("Bike is riding.");
}
}
public class Hierarchical {
public static void main(String[] args) {
// Creating an instance of Car
Cars myCar = new Cars();
myCar.start(); // Inherited from Vehicle
myCar.drive(); // Specific to Car
// Creating an instance of Bike
Bike myBike = new Bike();
myBike.start(); // Inherited from Vehicle
myBike.ride(); // Specific to Bike
}
}
Program 8: SuperConst
//Superclass
class Animals {
String name;
// Constructor of Superclass
public Animals(String name) {
this.name = name;
System.out.println("Animal constructor called");
}
}
//Subclass
class Dogs extends Animals {
String breed;
// Constructor of Subclass
public Dogs(String name, String breed) {
// Calling the superclass constructor using super()
super(name);
this.breed = breed;
//this.name=name;
//this.name=name;
System.out.println("Dog constructor called");
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Breed: " + breed);
}}
public class SuperConst {
public static void main(String[] args) {
// Creating an instance of Dog
Dogs myDog = new Dogs("Buddy", "Golden Retriever");
// Displaying the values
myDog.display();
}}