Object-oriented programming Interview questions in a single program

Object-oriented programming in Java 

Example program that demonstrates the use of Java OOP concepts including encapsulation, inheritance, and polymorphism in a single program:

Program 1:


// Encapsulation example - Account class with private data members and public methods to access them
class Account {
   private int accountId;
   private String accountHolderName;
   private double balance;

   public void setAccountId(int accountId) {
      this.accountId = accountId;
   }

   public int getAccountId() {
      return accountId;
   }

   public void setAccountHolderName(String accountHolderName) {
      this.accountHolderName = accountHolderName;
   }

   public String getAccountHolderName() {
      return accountHolderName;
   }

   public void setBalance(double balance) {
      this.balance = balance;
   }

   public double getBalance() {
      return balance;
   }

   // Polymorphism example - deposit() method with different parameter types
   public void deposit(double amount) {
      balance += amount;
      System.out.println(amount + " deposited successfully");
   }

   public void deposit(int amount) {
      balance += amount;
      System.out.println(amount + " deposited successfully");
   }

   // Inheritance example - SavingAccount class that extends Account class
   class SavingAccount extends Account {
      private double interestRate;

      public void setInterestRate(double interestRate) {
         this.interestRate = interestRate;
      }

      public double getInterestRate() {
         return interestRate;
      }

      public double calculateInterest() {
         return balance * interestRate / 100;
      }
   }
}

// Main class that uses Account and SavingAccount classes
public class Main {
   public static void main(String[] args) {
      // Encapsulation example - creating an Account object and setting its data members using public methods
      Account account = new Account();
      account.setAccountId(123);
      account.setAccountHolderName("John");
      account.setBalance(5000);

      // Polymorphism example - calling deposit() method with different parameter types
      account.deposit(1000);
      account.deposit(2000.50);

      // Inheritance example - creating a SavingAccount object and setting its data members using public methods
      Account.SavingAccount savingAccount = account.new SavingAccount();
      savingAccount.setInterestRate(2.5);
      savingAccount.setBalance(10000);

      // Calling calculateInterest() method to calculate interest on saving account balance
      double interest = savingAccount.calculateInterest();
      System.out.println("Interest earned: " + interest);
   }
}

In this program, we define an Account class that demonstrates encapsulation by having private data members (accountId, accountHolderName, and balance) and public methods (setAccountId(), getAccountId(), setAccountHolderName(), getAccountHolderName(), setBalance(), and getBalance()) to access them.

We also demonstrate polymorphism by having multiple deposit() methods with different parameter types and inheritance by having a SavingAccount class that extends the Account class and adds a interestRate data member and a calculateInterest() method to calculate the interest on the saving account balance.

In the Main class, we create an Account object and set its data members using public methods. We then call the deposit() method with different parameter types to demonstrate polymorphism. We also create a SavingAccount object and set its data members using public methods. We then call the calculateInterest() method to calculate the interest on the saving account balance and print the result.

Program 2:

Here's an example program that showcases the use of Encapsulation, Inheritance, Polymorphism, and Abstraction:


public class Main {
    public static void main(String[] args) {
        
        //Encapsulation Example
        Person person = new Person();
        person.setName("John");
        person.setAge(30);
        person.setAddress("123 Main St");
        System.out.println(person.toString());
        
        //Inheritance Example
        Student student = new Student();
        student.setName("Jane");
        student.setAge(20);
        student.setAddress("456 First Ave");
        student.setStudentId("123456");
        System.out.println(student.toString());
        
        //Polymorphism Example
        Animal animal1 = new Dog();
        animal1.speak();
        Animal animal2 = new Cat();
        animal2.speak();
        
        //Abstraction Example
        Shape shape1 = new Circle(5);
        System.out.println("Circle Area: " + shape1.getArea());
        Shape shape2 = new Rectangle(3, 4);
        System.out.println("Rectangle Area: " + shape2.getArea());
    }
}

//Encapsulation Example
class Person {
    private String name;
    private int age;
    private String address;
    
    public void setName(String name) {
        this.name = name;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    
    public void setAddress(String address) {
        this.address = address;
    }
    
    public String getName() {
        return name;
    }
    
    public int getAge() {
        return age;
    }
    
    public String getAddress() {
        return address;
    }
    
    public String toString() {
        return "Name: " + name + "\nAge: " + age + "\nAddress: " + address;
    }
}

//Inheritance Example
class Student extends Person {
    private String studentId;
    
    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }
    
    public String getStudentId() {
        return studentId;
    }
    
    public String toString() {
        return super.toString() + "\nStudent ID: " + studentId;
    }
}

//Polymorphism Example
class Animal {
    public void speak() {
        System.out.println("The animal makes a sound");
    }
}

class Dog extends Animal {
    public void speak() {
        System.out.println("The dog barks");
    }
}

class Cat extends Animal {
    public void speak() {
        System.out.println("The cat meows");
    }
}

//Abstraction Example
abstract class Shape {
    abstract double getArea();
}

class Circle extends Shape {
    private double radius;
    
    public Circle(double radius) {
        this.radius = radius;
    }
    
    public double getArea() {
        return Math.PI * radius * radius;
    }
}

class Rectangle extends Shape {
    private double length;
    private double width;
    
    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }
    
    public double getArea() {
        return length * width;
    }
}

 

In this program, we define a Main class that demonstrates the use of Encapsulation, Inheritance, Polymorphism, and Abstraction.

First, we show an example of Encapsulation by creating a Person class with private fields that can only be accessed through public setter and getter methods.

Post a Comment

0 Comments