Skip to main content

OOPS Java

package oOps;

import java.util.*;

public class StudentUse {


public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner scan = new Scanner(System.in);

Student s1 = new Student();

s1.name = "Divyanshu";

s1.rollNumber = 123;

Student s2 = new Student();

System.out.println(s1.name);


}


}



Question: What is oops and why to use oops ?
Ans: OOPs stands for object oriented programming language, the main purpose of oops is to deal with real world entity using programming language . or memory management .

OOPs ek code likhne ka tarika hai

OOPs Features :

  • Class - Class ek template hota hai jisme sirf ham logic define krte hai, class koi memory nhi leta hai 
  • object - Kisi ke class ka behaviour object hota hai object memory occupy krta hai.
  • Inheritance - Jitni bar chahe classes ko extend karke reuse kr skte hai.
  • Polymorphism - Ek hi object ka behaviour multiple ho skta hai .
  • Encapsulation
  • Abstraction - Jo information user ko show nhi karaani hai use hide kr dega aur jo show karaana hai whi show karega.

Access Modifier Private ----------

package oOps;


//classs is just like a form

public class Car {

int modelYear;

private String modelName;


public void setModelName(String mn) {

modelName = mn;

}

public String getModelName(String mn) {

return modelName;

}

/*if u want to access private variable then u need to use getter and setter*\


}


What is class in OOPS ?
Ans: 
1) Class is the collection of objects.
2) Class is not a real world intitiy , it is just a template or blueprint or prototype.
3) Class does not occupy memory.

 Method Advantage:
1) Code reusibuility 
2) Code optimization




Sabse phle ek class bna lenge 

lets suppose hamne ek student class bnana hai .

Student Class{

}


ab is class ke andar kon kon se object ya properties hongi wo dalne honge 

package oOps;


public class Student {

String name ;

}


isme ham ek name String type ka instance variable declare kr rhe 

Now, ham ek dusra class bnayenge same package k andar and where will call this class

Yaha ham uske objects bna rhe

package oOps;

import java.util.*;

public class StudentUse {


public static void main(String[] args) {

Student s1 = new Student();

s1.name = "Divyanshu"; //store divyanshu

System.out.println(s1.name);

}

}

1) hamne yha ek dusra class banaya hai aur jisme Student class ki s1 object create kr rhe
2) s1 object ke name variable me "Divyanshu" ka ek string store kr rhe
    Is line mein s1 object ke name variable mein "Divyanshu" string ko set kiya jata hai.
3) System.out.println ke madhyam se name variable ki value ko console par print kar rahe hain: System.out.println(s1.name);



Is line mein aap s1 object ke name variable ki value, jo ab "Divyanshu" hai, ko console par print kar rahe hain.



Constructor:

So first of all we need to use Class name (String n, int roll) {

name = n;
rollNumber = roll;

// another and best way approach to create constructor

this.name = n;
this.rollNumber = roll;



}

after than we need to call this class

Student s1 = new Student("Divyanshu", 123);
System.out.print(s1.name + s1.rollNumber);



NOTE: Constructor me Class name ki tarh same rhega

NOTE: Constructor function ki tarh hota hai

Example from eClipse  . . . >>>>

Creating Student class  . . . . .

package oOps;


public class Student {

String name ;

public int rollNumber ;


// public void setRollNumber(int r) {

// rollNumber = r;

// }

//

// Constructor

Student(String n , int roll){

this.name = n;

this.rollNumber = roll;

}


}


Calling this class in another class

package oOps;

import java.util.*;

public class StudentUse {


public static void main(String[] args) {

Student s1 = new Student("Divyanshu" , 123); // Store and call the values

Student s2 = new Student("New" , 124); // store and call the values according to constructor

// s1.name = "Divyanshu";

// s1.rollNumber = 100;

System.out.println(s1.name);

System.out.println(s1.rollNumber); // we are calling here and trying to print the value

System.out.println(s2.name + s2.rollNumber); //this is another way to call

// s2.name = "Yash";

// s2.rollNumber = 12;

// s2.setRollNumber(14);

// System.out.println(s2.name);

// System.out.println(s2.getRollNumber());

}

}


>>>Final Keyword in Oops JAVA
Ans:
Final is a modifier which provides restriction In JAVA .

>> This Keyword in Oops JAVA. 
"this" keyword in Java is used to refer to the current instance of a class. It has several purposes in object-oriented programming (OOP).


>>Components of OOPs - - -- - - -

  1. Inheritance
    Lets suppose Mujhe 2 class bnani hai first is Car and second is Bicycle ab inke beech kuch Functions and class commons honge jaise gear, max speed like these, inheritance kya krega basic and common class and things ko ek single class me store kr dega , mujhe common cheeje dubara nhi bnani pdegi.

    "extends" keyword Object-Oriented Programming (OOP) me ek important concept hai, jo primarily inheritance ko represent karta hai. Inheritance ek OOP principle hai jiski madhyam se ek class (jo subclass ya child class ke roop mein jaani jaati hai) dusri class (jo superclass ya parent class ke roop mein jaani jaati hai) ke properties aur methods ko inherit karti hai. "extends" keyword is inheritance ko establish karta hai Java aur kai dusre programming languages me

    Which keyboard is used to inherit the class ?
    Ans: extends
  2. If you Want to Print Parent Class properties as well as object class's properties In Inherit class so what u can do is ? 

    U can use super keyword 

    super.print();


    Iska basic format Java me is tarah hota hai:

    class ChildClass extends ParentClass {

    // Child class ki properties aur methods

    }


    package Vehicle;


    public class Vehicle { // this is Vehicle's Property

    String color;

    int maxSpeed;

    public void print() {

    System.out.println(color);

    System.out.println(maxSpeed);

    }


    }


    package Vehicle;


    public class VehicleUse { // here we can create object for vehicles like car, bicycle

    public static void main(String[] args) {

    // Object of vehicle

    Car c = new Car(); //this is object

    c.color = "black"; //

    c.nGear = 12;

    c.sCap = 19;

    c.maxSpeed = 120;

    c.print();

    c.cPrint();


    }


    }


    package Vehicle;


    public class Car extends Vehicle { //extends with Vehicle

    /*jo vehicle ki property hogi wo isme v aayegi*/


    int nGear ;

    int sCap ;

    public void cPrint() {

    System.out.println(nGear);

    System.out.println(sCap);

    }

    }



  3. Polymorphism
    Ans:
    Polymorphism is a greek word whose meaning is same object having different behaviour.

    Poly+manyphism
  4. Encapsulation  



Exception handling.

1) Will create a class for ecxeption handling 

Comments

Popular posts from this blog

Block vs Inline Elements In HTML

  Block: Block element always start in New Line  Block element always start in new line occupy complete width available  Example: h2, p, div Inline Element: They do not start in new line occuply width as much required  Example: img, anchor a,span There are no different between div and span, only difference is div is block element and span is Inline, it will help you to group your content or elements

Time Complexity

What is time complexity ? Ans: Time complexity not a run time of a code, its totally depends on system configuration, processor, server , input size will increase time complexity. Time complexity ko likhne ke tarike jo hote hai use notation kehte hai. Notation is basically symbol NOTATION OF COMPLEXITY Best Case: kam se kam time me chla gya code best case hota hai Ω (Omega) Ω(1) se likhte hai Average Case: Ek average time jisme code chle  Θ(n+1)/2 Worst Case: Worst case ka mtlb kuch bhi ho jaye is time ko ye exceed nhi kregea O(n) Nested Loop me Time Complexity multiply hoti hai aur n^2 banti hai Wahi Do different loops me N+M hoti hai jisme jiski sabse bdi value hai wo big of O me jati hai O(1) - Constant Time: Operations that take a constant amount of time, regardless of the size of the input data. O(log n) - Logarithmic Time: Algorithms that have a logarithmic time complexity, often seen in binary search or certain tree-based algorithms. This is best time Complexity this i...

Find Unique Number in Array Java , XOR

 Question:  WAP to find unique number in Array Java package aRray ; import java . util .*; public class practicalArray { public static void main ( String [] args ) { Scanner scan = new Scanner ( System . in ) ; // Step 1: Input size of the array from the user int size = scan . nextInt () ; // Step 2: Create an integer array of the specified size int [] arr = new int [ size ] ; // Step 3: Initialize 'res' with the first element of the array int res = arr [ 0 ] ; // Step 4: Loop to read array elements and find the unique number for ( int i = 1 ; i < size ; i ++ ) { // Read an integer from the user and store it in the array arr [ i ] = scan . nextInt () ; // Step 5: Use XOR (^) operation to find the unique number res = res ^ arr [ i ] ; } // Step 6: Print the unique number System . out . print...