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

Python Final Lectures

 Q- how to Print Hello World print("Hello World") Variables in python ------- age = 30   #variable should be intutive so that we can learn any time print(age) Note: Shift+Enter is shortcut to run command 2) ' #' this is for writing the comment in python Rules for Variables--- Variable can not be start with any number like - 1age  Number can use in between and end with variable like - age1 age2 Special characters are not allowed expect _ (underscore) like - age_my Space not allowed in variable  Python is case sensitive  Way to define Variable --- age1,age2 = 30,25  age1 = 30 age2 = 25 age1=age2=30   #if 30 age for both variable   >> Data type the type of data is basically data type Integer = age1 to age3 is basically integer   , Integer is basically full number lets check = type(age1)  #it will give u print int float=  basically decimal values Interest =  30.24 type(Interest) #answer is float Message = ...

Add CSS using external CSS

>>> U just need to create a another page and save it with the name style.css >>> and then go to link that style page with your html docs how to link your css with html page ? >>> You can find code below , it will help you to link your external page with your html docs <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Divyanshu Khare || MERN Developer</title> <meta description = "description" content="Divyanshu Khare's website"> <link rel="stylesheet" type="text/css" href="style.css">   <!----------link external css page ---------> </head> <body> </body> </html>

Python

Indexing--  it will help u to fetch single character  string= "Python" string[2] # slicing process of fetching a sub-string from the given string  #sequence of charater we can fetch means more than 1 character string="Divyanshu" string[2:4+1]   #basically here number is index value #string[start_index:end+index+1] string = "Hunny"   #indexing agr positive hai to left se count hoga #right se negative me string[:]  #it will give u entire string #now If i want to any characterwise index like string is Hunny and I want un only #string[start_index:end_index+1:step] string[1:4:2] #reverse your string #string[:: -1] string="Baba hunny" string[:: -1] # to convert into lowecase string="New Divyanshu" new_string=string.lower()  #new_string becase we've to create another string print(new_string) s1={1,2,3,4,5}    s2={3,2,8,67,85} s3=s1.union(s2) s3   #isme add hota hai whole value lekin common value update nhi hongi #intersection - ...