OOPS - Java
Wednesday, 21 May 2025 16:56
Object Oriented Programing -> -> process - classes and object ->
POP - Procedure Oriented Programming -> specific task by task chl rhe the
OOPS -> object oriented Programming -> class and Object ->
Classes Objects
Class is a logical entity Real world entity
Class consist of attributes Object is a provide or a template of class
and behaviors Blue print of class
Which given by the objects
Class consists mainly Object just created for a the class so the new new
user can come and use the functionality
Properties ---- Data
members
Method ----- Functions
Student { Object -> dubara apni value provide
Roll Class ko
Name Class k pass template uske details
} Object uss class ka object baanne ke baad woh puri
functionality use kr saktha hia
Setters and Getters ->
Setter -> to set the value
Constructor -> special method -> automatically called -> at the time of object creation ->
Rules ->
1. Constructor name should be same name as class name name
2. Constructor can not return any value
3. Constructor automatically called at the time of memory allocation
4. Types =>
Pillars -> Data members
Encapsulation
polymorphism
Inheritance
Data Abstraction
Function
Class Student
Encapsulation ->
Wrapping up of data/properties with method/function - together into a single
class/unit
Class Student{
String name;
Int marks ;
String ->
Getname()
Class Student{
String name;
Int marks ;
String ->
Getname()
Getavg(){. // calculation
-----------
}
Polymorphism ->
When a method in a class has work more than one form ->
Example ->
Dhoni -> in a single match
1. WK ->
2. Bowling ->
3. Batting ->
Polymorhpism divided into 2 cateogires
1. Compile time
2. Run time
i. Compiler -> Method overloading
ii. Runtime -> Method Overriding
Method overloading -> in which we have multiple method name , with same name but diff
parameters
Class Cal{
Int sum(int a , int b){
Return a+b;
}
Int sum(int a , int b , int c){
Return a+b+c;
}
Float sum(float a , float b){
Return a+b;
}
Method Overriding ->
When child class has same method name with parents class and also that child
extending parents , so child class property will get the first priority so that that
method override
When u have same method but diff -> and also extend
Class Parent{
Void hello(){
SYSO("HELLO Parents ");
}
Class Child extends Parent{
Void hello(){
Syso("hello child");
}
}
Class Child extends Parent{
Void hello(){
Syso("hello child");
}
Child c = new Child();
C.hello() -> // Hello parents -> after adding that same method in child so that
// the answer will be of child class
Inheritance ->
Parent -> base
Child -> sub class -> derived class
When I have one class with 5 property and I want that same property in another
class
But I didn’t want to write again that property -> so in this case we inheritance
Inheritance I extends the second class with the first class which have the 5
property , on the spot that property will be inherit by the second one ,
So that without writing again the method we can utilise that functionality
Reduce redubancy ->
1 single extend -> multiple method utilise
Single
Multi
Hierarchal
Class Animal{
Hybrid
Void speak(){ Multiple Inheritance ->
Cout<<"Hello"; never work in java directly
}
Void eat(){
Cout<<"eating";
}
Class Dog extends Animals{ // By making Dog object ,dog can implement all the
property of animal ->
Animal
Void speak(){
Cout<<"barking";
Multi level ->
Herirical
Void Breed(){
Cout<<"german"; Animal
Dog }
Level -> single level
Multi level
Dog extends Animal
BabyDog Cat extend Animal
Hybrid
Hybrid
Single level A
Multi level B
Herarichal Multiple
Hybrid
Multiple ->
Is not allowed in java
language ???
C
C extends A,B{
Constructor ->
1. Special method -> automatically call -> object creation ->
2. Iska name same hota hai class k naam se
3. No return -> memory allocated at the time of object creation
Class Student{
//data members
//Student s = new Student();
String name;
Int age;
Student s1= new Student(10);
S.setname
S.get
// getters and setters
Student(){
Cout<<"Constructor is called";
}
Student(int age){
Cout<<"your age is " , age;
}
Constructor ->
1. Paramteretized
2. Non Parameterized ->
Data Abstraction ->
Hiding the important detail => and providing you the functionality to the user ->
Method -> aapka kaam krdete hai
Method ko call krna hia
Jaise -> car ->
Engine start
Acc
Gear ->
Cout<<"Car move";
Jaise -> car ->
Engine start
Acc
Gear ->
Cout<<"Car move";
Example ->
Person 1
Person 2
SMS
SMS
Person 1 send the message
Person 2 receive a message
Bank Account ->
Account ->
Cash Withrawal -> 1000 ->
Cust account debit -> 1000
Bank Account credit -> 1000
Cash in hand ->
Code ->
Abstract Class Interface
Cannot abstract methods Specific set of class is implemented , method , abstract are
Cannot be instanized , without both allowed
implementation
Class can inherit from on abstract class Class can implemented from multiple interface ( Multiple
Inheritance )
Method -> public , private dono chalthe Method implictly public
hai
Can have member - final , non final , Implicity public -> static and final
static, static
Final -> Reassigned , re - initialized
Static -> we can reassign
Final -> Cannot be overridden by subclass
Static -> static member ko hi use krenge but overriden
Final -> first to last remain no change
Static local variable -> java allowed -> C / C++
Multiple Inheritance -> java do not allows to extend more than one class
Dog
BabyDog
Static local variable -> java allowed -> C / C++
Multiple Inheritance -> java do not allows to extend more than one class
Dog
BabyDog
Animal
But we can use Multiple Inheritance -> Data Abstraction ->
Abstract classes
Interfaces