Thursday, November 16, 2023

oops concept in java

 OOPS

Java is object oriented programming language means in java everthing is in the form of class and object.

java mainly depends on four pillars like

1. abstraction 

2.encapsulation 

3.inheritence

4.polymorphism

Abstraction

abstraction is process of hiding implementation and only showing functionality to the users is called abstraction .


The abstract keyword is a non-access modifier, used for classes and methods:

Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).

Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).
 Realtime example:-atm ,sms chatbox etc
example :-without inheritence

abstract class Animal { 
public abstract void animalSound();
 public void sleep() {
 System.out.println("Zzz"); 
}
 }


example :-with inheritence

abstract class Animal {

public abstract void animalSound(); 
 public void sleep()
 System.out.println("Zzz");
 }
 } 

 class Elephant extends Animal {
 public void animalSound() { 
 System.out.println("The  Elephant  says: ooo ooo"); 
class Main {
 public static void main(String[] args) {
 Elephant   elephant  = new  Elephant (); 

 elephant.animalSound(); 
 elephant.sleep(); } 
}


Encapsulation

It is a process of binding variables and methods in single class .These variables are private   by default and cannot accessible out side the class.
It is 100% not secure.
Real time example mobile :- interaction between mobile and user.
Example

public class Person { 

private String name; 
private int age;

public Person(String name, int age) { 
this.name = name;
 this.age = age; 
}

public String getName() {
 return name; 

 public void setName(String name) {
 this.name = name; 

 public int getAge() { 
return age; 
}
 
 public void setAge(int age) { 
if (age >= 0) {
 this.age = age; 
 }
 }
 } 
public class Main { 
public static void main(String[] args) {
Person person = new Person("John", 30); 

System.out.println("Name: " + person.getName()); 
System.out.println("Age: " + person.getAge());

person.setName("Alice"); 
person.setAge(25); 

System.out.println("Updated Name: " + person.getName()); 
System.out.println("Updated Age: " + person.getAge()); 
}
 }

Inheritence
Inheritence is a process of aquiring properties from one class to another class using extend keyword.
The class which is providing properties is called super class or parent class.
The class which is aquiring properties from super class is called sub class or chield class.

Types of Inheritence
1.single level inheritence:- 1 super class and 1 sub calss
2.multilevel inheritence:- 1,2,3  - - ,n classes extended in one super and 1 sub class manner untill n class.
3.heirarchy inheritence:- 1 super class an d 2 sub class
4.hybrid inheritence:-combination any 2 type of inheritence.
5.multiple inheritence:- 2 super class an d 1 sub class which is not supported by java so thats they are using interface concept.



Example for inheritence

class Vehicle {
 protected String brand = "maruti";


public void honk() { 
 System.out.println("Tuut, tuut!"); 
}
 } 

class Car extends Vehicle {
private String modelName = "Maruti800";

 public static void main(String[] args) { 
 Car myCar = new Car(); 
 myCar.honk();
System.out.println(myCar.brand + " " + myCar.modelName); } 
}


Polymorphism
 If one object is behaving in different characters in different lifecycle of project is called as polymorphism.

It is achived using two diffrent ways

1.RunTime polymorphism
2.CompileTime polymorphism


RunTime polymorphism:-
Runtime polymorphism is allso called as dynamic polymorphism beacuse the overriden methods are called during run time .
In this process, an overridden method is called through the reference variable of a superclass.
  example:-Bank
class Bank{
float getRateOfInterest(){
return 0;
}  
class HDFC extends Bank{
float getRateOfInterest(){
return 8.4f;
}  
}  
class SBI extends Bank{  
float getRateOfInterest(){
return 7.3f;
}  
}  
class AXIS extends Bank{  
float getRateOfInterest(){
return 9.7f;
}  
}  

class TestPolymorphism{  

public static void main(String args[]){  
Bank b;  
b=new HDFC();  
System.out.println("HDFC Rate of Interest: "+b.getRateOfInterest());  

b=new SBI();  
System.out.println("SBI Rate of Interest: "+b.getRateOfInterest());  

b=new AXIS();  
System.out.println("AXIS Rate of Interest: "+b.getRateOfInterest());  
}  
}  




CompileTime polymorphism
The compiletime polymorphism is allso called as static binding because method binding will be done during compilation only.
There is no concept of inheritence .
In Java, compile-time polymorphism is mostly achieved by method overloading.

example:- simple calculator

public class Calculator {
public int add(int x, int y) {
return x + y;
}
public double add(double x, double y) {
return x+ y;
}
public static void main(String[] args) {
Calculator calculator = new Calculator();

int sum1 = calculator.add(5, 10);
System.out.println("sum of integers (integers): " + sum1);

double sum2 = calculator.add(2.5, 3.7);
System.out.println("Sum of  doble numbers(doubles): " + sum2);
}
}



No comments:

Post a Comment