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:
Name:
- The constructor has the same name as the class.
No Return Type:
- Unlike regular methods, constructors do not have a return type, not even
void
.
- Unlike regular methods, constructors do not have a return type, not even
Invocation:
- Constructors are automatically invoked when an object is created using the
new
keyword.
- Constructors are automatically invoked when an object is created using the
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.
Overloading:
- Like regular methods, constructors can be overloaded, which means a class can have multiple constructors with different parameter lists.
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
Post a Comment