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

Polity

  1.    सन 1600 में ईस्ट इंडिया कंपनी भारत आई थी जिसका परमिशन ब्रिटिश की महारानी एलीजाबेथ ने दिया था 2.    परमिशन में चार्टर दिया गया था साथ ही मोनोपोली दी गयी थी अलीजाबेत के द्वारा 3.    बिटिश ईष्ट इंडिया कंपनी भारत शिप से आई थी जिस शिप का नाम था रेड ड्रैगन 4.    भारत में आने के बाद उन्होंने पहली फैक्ट्री 1611 मछलीपटनम में बनाई 5.    दूसरी फैक्ट्री 1612 में सूरत में बनाया था 6.    फैक्ट्री नियन्त्र के लिए तीन प्रेसीडेंसी बनायीं गयी जो थी बॉम्बे, बंगाल, मद्रास 7.    बंगाल का राजा था सिराजुदुल्ला और ब्रिटिश रोबर्ट clive युद्ध किया 1757 ऐसा जिसे battle of plasi कहा गया जिसमें रोबर्ट clive की जीत हुयी 8.    कंपनी का rule 1773 से 1858 तक चला था 9.    ताज का शाशन था 1858 से 1947 10.    Regulating act आया था 1773 में 11.    Act of settlement आया था 1781 में 12.    भारत परिषद् अधिनियम आया था 1861, 1892, 1909 13.    Govt of इंडिया act आया था 1858 में 14.                  ब्रिटिश सरकार ने 1773 में एक regulating act लाया गया जिसमें बंगाल को हेड बनाया गया जिसे गवर्नर जनरल कहा गया बंगा

Linked List Data Structure

Question: How to create without generic Int type Node ? Ans:  public class Node { // this is Node class without Generic int data ; // this is for data like array Element Node next ; //Node ek class hai , usi class ka khud ka variable hai, This is Node(Class) Type variable for //Node is basically refer to class , this is for next element Node ( int data ){ // this is constructor bcse user will pass data value and int because we want to create int type data constructor this . data = data ; // this is refer data next = null ; } }  

Test 2

 Question: You have made a smartphone app and want to set its subscription price such that the profit earned is maximised. There are certain users who will subscribe to your app only if their budget is greater than or equal to your price. You will be provided with a list of size N having budgets of subscribers and you need to return the maximum profit that you can earn. Lets say you decide that price of your app is Rs. x and there are N number of subscribers. So maximum profit you can earn is : m*x Sample input 1: 30 20 53 14 Output 60 import   java . util .*; import   java . util . Scanner ; public   class   solution   {      public   static   int   maximumProfit ( int   budget [])   {      Arrays . sort ( budget );          // int maxProfit = 0;          // // Iterate through each possible subscription price and calculate profit          // for (int i = 0; i < budget.length; i++) {          //     int currentProfit = budget[i] * (budget.length - i);          //     maxProfit = Mat