Program1:Inheritance
//Base class
class Vehicle {
void start() {
[Link]("The vehicle is starting.");
}
}
//Derived class
class Car extends Vehicle {
void honk() {
[Link]("The car is honking.");
}
}
//Main class to test the inheritance
public class inherit3 {
public static void main(String[] args) {
Car myCar = new Car();
[Link](); // Inherited method from Vehicle
[Link](); // Method from Car
}
}
Output:
The vehicle is starting.
The car is honking.
Program2: :Inheritance
//Base class
class Animal {
void eat() {
[Link]("This animal eats food.");
}
}
//Derived class
class Dog extends Animal {
void bark() {
[Link]("The dog barks.");
}
}
//Main class to test the inheritance
public class inherit2 {
public static void main(String[] args) {
Dog myDog = new Dog();
[Link](); // Inherited method from Animal
[Link](); // Method from Dog
}
}
Output:
This animal eats food.
The dog barks.
Program 3: Inheritance with constructor
//Base class
class Person {
Person() {
[Link]("Person constructor called.");
}
}
//Derived class
class Student extends Person {
Student() {
[Link]("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();
[Link]("rate of interest of sbi is :"
+[Link]());
ICIC i= new ICIC ();
[Link]("rate of interest of icic is :" +
[Link]());
Bank b= new SBI();
[Link]("rate of interest of sbi is :" +
[Link]());
}
}
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) {
[Link] = 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
[Link]("Tesla Model S");
// Accessing the private member using the getter
[Link]("Car model: " + [Link]());
}
}
Program 5:
public class privateerror {
public static void main(String[] args) {
B b =new B();
[Link]();
}
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:
//[Link]
class BankAccount {
protected double balance;
public BankAccount(double initialBalance) {
if (initialBalance >= 0) {
[Link] = initialBalance;
} else {
[Link] = 0;
[Link]("Initial balance can't be negative. Set to 0.");
}
}
// Method to deposit money
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
[Link]("Deposit successful. New balance: $" + balance);
} else {
[Link]("Deposit amount must be positive.");
}
}
// Method to withdraw money
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
[Link]("Withdrawal successful. New balance: $" +
balance);
} else {
[Link]("Invalid withdrawal amount. Current balance: $" +
balance);
}
}
// Method to check the balance
public double getBalance() {
return balance;
}
}
//[Link]
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;
[Link]("Withdrawal successful. New balance: $" +
balance);
} else {
[Link]("Withdrawal failed. Minimum balance of $100
required. Current balance: $" + balance);
}
}
}
//[Link]
public class Main {
public static void main(String[] args) {
// Create a regular bank account
BankAccount myAccount = new BankAccount(500);
[Link](100);
[Link](200);
[Link](600); // Invalid withdrawal
// Create a savings account
SavingsAccount mySavings = new SavingsAccount(500);
[Link](150);
[Link](400); // Should succeed
[Link](200); // Should fail due to minimum balance
}
}
Program 7: Multilevel
//Base class (Grandparent)
class Animal {
public void eat() {
[Link]("This animal eats food.");
}
}
//Derived class (Parent) that inherits from Animal
class Mammal extends Animal {
public void breathe() {
[Link]("This mammal breathes air.");
}
}
//Derived class (Child) that inherits from Mammal
class Dog extends Mammal {
public void bark() {
[Link]("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
[Link](); // From Animal class
[Link](); // From Mammal class
[Link](); // From Dog class
}
}
Program 8: Hierarchical
/// Parent class
class Vehicle {
public void start() {
[Link]("Vehicle is starting.");
}
}
// Child class 1
class Cars extends Vehicle {
public void drive() {
[Link]("Car is driving.");
}
}
// Child class 2
class Bike extends Vehicle {
public void ride() {
[Link]("Bike is riding.");
}
}
public class Hierarchical {
public static void main(String[] args) {
// Creating an instance of Car
Cars myCar = new Cars();
[Link](); // Inherited from Vehicle
[Link](); // Specific to Car
// Creating an instance of Bike
Bike myBike = new Bike();
[Link](); // Inherited from Vehicle
[Link](); // Specific to Bike
}
}
Program 8: SuperConst
//Superclass
class Animals {
String name;
// Constructor of Superclass
public Animals(String name) {
[Link] = name;
[Link]("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);
[Link] = breed;
//[Link]=name;
//[Link]=name;
[Link]("Dog constructor called");
}
public void display() {
[Link]("Name: " + name);
[Link]("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
[Link]();
}}