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 - - -- - - -
- 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 - 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);
}
}
- Polymorphism
Ans: Polymorphism is a greek word whose meaning is same object having different behaviour.
Poly+manyphism - Encapsulation
Exception handling.
1) Will create a class for ecxeption handling
Comments
Post a Comment