Skip to main content

OOPs 2 JAVA

What is OOPS?

Ans:Lets say my college has 10 students and each students will have some property like age, name and roll Number. Now every student have these 3 properties , so what I will do in normal term , I'm creating a student 1 and I'll say student1 have these 3 property ,every time when I'll create a student I'll say every student have these 3 property but this is not a efficient solution, we dont want to add that.

Instead of this what we will do will create a seperate class or just like a form and template , okay so student ? so whatever student is, his age will be, his name will be, his roll number will be. Now you want to create another students lets use this template to use these copy.


So hamne ek blueprint or template banake rakh diya hai which we have going to create actual student object

this is just a formula of a student Like uski property. In object oriented programming will say class , This is a class each of these individual instance is a OOPS.

Student Class Template >>



public class Student {

/*everystudent will have these property
these are template for student if I'll create a student ,
student will have these properties, if i will create a any student,
then student will have these properties*/

String name;
int rollNUmber;
int age;
}
/*everystudent will have these property
these are template for student if I'll create a student ,
student will have these properties, if i will create a any student,
then student will have these properties*/

here I'm going to create student according to our Template

public class StudentOBJ {

public static void main(String[] args){

//we have created object of student
Student s1 = new Student(); //we are creating new student with the name s1
/*lets add properties of this s1 student which is available on template*/
s1.name = "Divyanshu";
s1.rollNUmber = 92;
s1.age = 18;

/*but here is a problem this properties are not required, we can use only
1 property of student like i want to add only name of s1 student so we can leave other
properties created on Student class
*/
Student s2 = new Student(); //this is second student
s2.name = "Divyanshu";
s2.rollNUmber = 92;
s2.age = 18;
System.out.println(s1);
}

}
  /*but here is a problem this properties are not required, we can use only
1 property of student like i want to add only name of s1 student so we can leave other
properties created on Student class
*/


Access Modifier (Private, Public, Default):

Like I dont want to give access to anyone to change my roll number outside the package then what I can do is , i can just crate private. by default its all on within the package .

default: Default means within the package.

public: public means from anywhere can access 

private: within the class 


Like here I'm going to create private to my rollNumber:



public class Student {

/*everystudent will have these property
these are template for student if I'll create a student ,
student will have these properties, if i will create a any student,
then student will have these properties*/

String name;
private int rollNUmber;
int age;
}


StudentOBJ Showing errro below👇👇

public class StudentOBJ {

public static void main(String[] args){

//we have created object of student
Student s1 = new Student(); //we are creating new student with the name s1
/*lets add properties of this s1 student which is available on template*/
s1.name = "Divyanshu";
s1.rollNUmber = 92; // this is showing error bcse of private
s1.age = 18;

/*but here is a problem this properties are not required, we can use only
1 property of student like i want to add only name of s1 student so we can leave other
properties created on Student class
*/
Student s2 = new Student(); //this is second student
s2.name = "Divyanshu";
s2.rollNUmber = 92; // this is showing error bcse of private
s2.age = 18;
System.out.println(s1);
}

}



How to use private rollNumber (Getter, Setter):



public class Student {

/*everystudent will have these property
these are template for student if I'll create a student ,
student will have these properties, if i will create a any student,
then student will have these properties*/

String name;
private int rollNumber;
int age;


/*creating a getter function to resolve this private error*/

public int getRollNumber(){ //return type is int bcse function is for get number
return rollNumber;

}

//creating setter funtion to set rollNumber access issue

public void setRollNumber(int rn){ //you can create any name of this function
/*in parameter need to give that parameter which i want to set like int, string and 
u can set any name for this parameter*/

rollNumber = rn;
}

}




public class StudentOBJ {

public static void main(String[] args){

//we have created object of student
Student s1 = new Student(); //we are creating new student with the name s1
/*lets add properties of this s1 student which is available on template*/
s1.name = "Divyanshu";
s1.setRollNumber(92); // this is way to set and modify roll Number in private
s1.age = 18;

/*but here is a problem this properties are not required, we can use only
1 property of student like i want to add only name of s1 student so we can leave other
properties created on Student class
*/
Student s2 = new Student(); //this is second student
s2.name = "Divyanshu";
s2.setRollNumber(191); // this is way to set and modify roll Number in private
s2.age = 18;
System.out.println(s1.getRollNumber()); //this is way get to print rollNumber in private
}

}
by using getter setter u can access template's properties in same package
What is Constructor in JAVA ?

Ans: In Java, a constructor is a special type of method that is used to initialize objects of a class. It has the same name as the class and is called automatically when an object of the class is created. The primary purpose of a constructor is to set initial values for the instance variables of the object or to perform any other necessary setup tasks.

Key characteristics of constructors in Java:

  1. Name:

    • The constructor has the same name as the class.
  2. No Return Type:

    • Unlike regular methods, constructors do not have a return type, not even void.
  3. Invocation:

    • Constructors are automatically invoked when an object is created using the new keyword.
  4. Initialization:

    • Constructors are often used to initialize the instance variables of the object or perform other setup tasks needed for the object to function correctly.
  5. Overloading:

    • Like regular methods, constructors can be overloaded, which means a class can have multiple constructors with different parameter lists.
Why to use Constructor ?
Ans:  So what I actually want is if I'm creating a object student that should be a property like roll number, Age, Name and I should not allow anybody to create object (student) without these properties so in this case constructor will help.

Like maine ek student create kiya uska koi name hi nhi hai, rollNumber hi nhi hai so ham use null nhi rakh ke what I want is whenever create a student , student have these properties so this is basically part of constructor.

Constructor create on Student class>>



public class Student {

/*everystudent will have these property
these are template for student if I'll create a student ,
student will have these properties, if i will create a any student,
then student will have these properties*/

String name;
private int rollNumber;
int age;


/*creating a getter function to resolve this private error*/

public int getRollNumber(){ //return type is int bcse function is for get number
return rollNumber;

}

//creating setter funtion to set rollNumber access issue

public void setRollNumber(int rn){ //you can create any name of this function

rollNumber = rn;
}



//constructor

public Student(String str, int rll, int ag){ //this is constructor
//the constructor has same name as the class name(Student)
//we can create parameter with another name with data type
//this. bcse it will indicate direct to our class
this.name = str;
this.age = ag;
this.rollNumber = rll;
//after this you cant create object without these property

}
}


Now how to create student using constructor ?

public class StudentOBJ {

public static void main(String[] args) {

//we have created object of student s1
Student s1 = new Student("Divyanshu", 92, 18);
/*we are creating new student with the name s1 but we need to pass
value of our constructor, in my constructor name,roll number and age is created
thats why we need to pass value for these
*/

}
}


  /*we are creating new student with the name s1 but we need to pass
value of our constructor, in my constructor name,roll number and age is created
thats why we need to pass value for these
*/

s

Question: Can we create private to class ?

Ans: We cant create private to class 

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...