Java basics OOPs With Example

Abstraction  

Abstraction is a key concept in object-oriented programming that allows you to hide unnecessary details and show only the relevant information to the user. In Java, abstraction is typically achieved through the use of abstract classes and interfaces.

Here is an example program that demonstrates the use of abstraction in Java:


abstract class Shape {
   protected String color;
   
   public Shape(String color) {
      this.color = color;
   }
   
   public abstract double area();
   
   public abstract String toString();
}

class Circle extends Shape {
   protected double radius;
   
   public Circle(String color, double radius) {
      super(color);
      this.radius = radius;
   }
   
   public double area() {
      return Math.PI * radius * radius;
   }
   
   public String toString() {
      return "Circle color is " + color + " and area is " + area();
   }
}

class Rectangle extends Shape {
   protected double length;
   protected double width;
   
   public Rectangle(String color, double length, double width) {
      super(color);
      this.length = length;
      this.width = width;
   }
   
   public double area() {
      return length * width;
   }
   
   public String toString() {
      return "Rectangle color is " + color + " and area is " + area();
   }
}

public class Main {
   public static void main(String[] args) {
      Shape s1 = new Circle("Red", 2.2);
      Shape s2 = new Rectangle("Yellow", 2, 4);
      
      System.out.println(s1.toString());
      System.out.println(s2.toString());
   }
}

In this program, we define an abstract class called Shape that has a color field and two abstract methods: area() and toString(). We then define two concrete classes called Circle and Rectangle that extend the Shape class and implement their own versions of the area() and toString() methods. 

 In the Main class, we create two objects, one of type Circle and one of type Rectangle, and assign them to variables of type Shape. We then call the toString() method on each object, which returns a string representation of the object's color and area. 

 This demonstrates the principle of abstraction, as we are able to use the Shape class to represent both Circle and Rectangle objects, without having to worry about the specific details of each object. Instead, we are able to focus on the common properties and behaviors of all shapes, such as their color and area.

Encapsulation 

Encapsulation is a principle in object-oriented programming that involves bundling data and the methods that operate on that data within a single unit, called a class. In Java, encapsulation is typically achieved through the use of access modifiers, such as private, protected, and public.

Here is an example program that demonstrates the use of encapsulation in Java:



public class BankAccount {
   private int accountNumber;
   private String accountHolderName;
   private double balance;
   
   public BankAccount(int accountNumber, String accountHolderName) {
      this.accountNumber = accountNumber;
      this.accountHolderName = accountHolderName;
      this.balance = 0.0;
   }
   
   public int getAccountNumber() {
      return accountNumber;
   }
   
   public String getAccountHolderName() {
      return accountHolderName;
   }
   
   public double getBalance() {
      return balance;
   }
   
   public void deposit(double amount) {
      balance += amount;
   }
   
   public void withdraw(double amount) {
      if (balance >= amount) {
         balance -= amount;
      } else {
         System.out.println("Insufficient balance");
      }
   }
}

public class Main {
   public static void main(String[] args) {
      BankAccount account = new BankAccount(12345, "John Doe");
      account.deposit(500.0);
      account.withdraw(200.0);
      System.out.println("Account number: " + account.getAccountNumber());
      System.out.println("Account holder name: " + account.getAccountHolderName());
      System.out.println("Account balance: " + account.getBalance());
   }
}

In this program, we define a class called BankAccount that has three private fields: accountNumber, accountHolderName, and balance. We also define a constructor that initializes the accountNumber and accountHolderName fields, and sets the balance field to zero.

We then define four public methods: getAccountNumber(), getAccountHolderName(), getBalance(), deposit(), and withdraw(). The get methods allow us to retrieve the values of the private fields, while the deposit() and withdraw() methods allow us to modify the balance field in a controlled manner.

In the Main class, we create a BankAccount object and perform some transactions, such as depositing and withdrawing money. We then use the get methods to retrieve the values of the private fields, which demonstrates how encapsulation allows us to control access to the data and methods of a class.

Overall, this program demonstrates how encapsulation can be used to create secure and well-structured Java programs that are easy to maintain and extend.

Inheritance 

Inheritance is a key concept in object-oriented programming that allows one class to inherit properties and behaviors from another class. In Java, inheritance is achieved through the use of the extends keyword.

Here is an example program that demonstrates the use of inheritance in Java:



class Animal {
   private String name;
   
   public Animal(String name) {
      this.name = name;
   }
   
   public void speak() {
      System.out.println("The " + name + " makes a sound");
   }
}

class Dog extends Animal {
   public Dog(String name) {
      super(name);
   }
   
   public void speak() {
      System.out.println("The " + super.name + " barks");
   }
}

class Cat extends Animal {
   public Cat(String name) {
      super(name);
   }
   
   public void speak() {
      System.out.println("The " + super.name + " meows");
   }
}

public class Main {
   public static void main(String[] args) {
      Animal animal = new Animal("Animal");
      Dog dog = new Dog("Rufus");
      Cat cat = new Cat("Whiskers");
      
      animal.speak();
      dog.speak();
      cat.speak();
   }
}

In this program, we define a class called Animal that has a private field called name and a speak() method that prints a generic message to the console. 

 We then define two sub-classes of Animal called Dog and Cat that inherit the name field and speak() method from the parent class. Each sub-class overrides the speak() method to provide a specific message for the animal. 

 In the Main class, we create objects of type Animal, Dog, and Cat and call the speak() method on each object. This demonstrates how the sub-classes inherit the properties and behaviors of the parent class, and can be used to extend and specialize the functionality of the parent class. 

 Overall, this program demonstrates how inheritance can be used to create hierarchical and modular Java programs that are easy to understand and maintain.

Post a Comment

0 Comments