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

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