Skip to content

Commit fb463f9

Browse files
author
Vinay Patel
committed
OOP CONCEPT
1 parent 9a95ee8 commit fb463f9

File tree

22 files changed

+641
-0
lines changed

22 files changed

+641
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package OOP_KUNAL;
2+
3+
import java.util.Arrays;
4+
5+
public class Main {
6+
public static void main(String[] args) {
7+
8+
int[] numbers = new int[5];
9+
String[] names = new String[5];
10+
11+
Student[] students = new Student[5];
12+
13+
// just declaring
14+
// Student vinay;
15+
// System.out.println(Arrays.toString(students)); // Output is null
16+
17+
// vinay = new Student();
18+
// here new keyword use it --> Dynamically allocate the memory and return a ref
19+
// to it.
20+
21+
Student vinay = new Student(197, "VP", 90);
22+
23+
// System.out.println(vinay); // give random value
24+
25+
// vinay.rno =197;
26+
// vinay.name="Vinay Patel";
27+
// vinay.marks = 88.54f;
28+
29+
// vinay.changeName("Patel");
30+
// vinay.greeting();
31+
32+
System.out.println(vinay.rno); // by default 0
33+
System.out.println(vinay.name); // by default null
34+
System.out.println(vinay.marks); // by default 0.0
35+
36+
// constructure is defined that what is happen when you creating object.
37+
System.out.println();
38+
Student random = new Student(vinay);
39+
System.out.println(random.name);
40+
System.out.println(random.marks);
41+
System.out.println(random.rno);
42+
43+
}
44+
45+
}
46+
// create a class
47+
48+
// Inside the object variable is known as instance variable.
49+
class Student {
50+
int rno;
51+
String name;
52+
float marks = 90;
53+
54+
// we need a way to add the value of the above properties object by objects.
55+
56+
// we need one word to access every object.
57+
58+
void greeting() {
59+
System.out.println("Hello ! my name is " + name);
60+
}
61+
62+
void changeName(String newName) {
63+
// name = newName;
64+
this.name = newName;
65+
}
66+
67+
Student(Student other) {
68+
this.name = other.name;
69+
this.rno = other.rno;
70+
this.marks = other.marks;
71+
}
72+
73+
Student() {
74+
// vinay.rno =197;
75+
// vinay.name="Vinay Patel";
76+
// vinay.marks = 88.54f;
77+
this.rno = 197;
78+
this.name = "Vinay Patel";
79+
this.marks = 88.54f;
80+
// replacing name with referance vaibale.(Simple meaning of this keyword)
81+
}
82+
83+
// this keyword is simple means that is referance to the object.
84+
85+
// here below aprit is object of student class and this is automatically take
86+
// name (Arput.rno,Arpit.name)
87+
88+
// Student arpit = new Student(17,"Arpit")
89+
Student(int roll, String name, float marks) {
90+
this.rno = roll;
91+
this.name = name;
92+
this.marks = marks;
93+
}
94+
// Student(){
95+
96+
// }
97+
}
98+
99+
// premetive data type like int ,float are stored in stack memory only for more
100+
// effeciency
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// premetive data types should be also created using new keyword is known as wrapper class.
2+
3+
// In java only pass by value .
4+
5+
package OOP_KUNAL;
6+
7+
import javax.print.DocFlavor.STRING;
8+
9+
public class WrapperExample {
10+
public static void main(String[] args) {
11+
// int a = 10;
12+
// int b=20;
13+
// Integer num = new Integer(45);
14+
15+
// Integer num = 45; //here num is like object
16+
// num.compare(a, a) // different method is available.
17+
18+
Integer a = 10;
19+
Integer b=20;
20+
21+
22+
23+
swap(a, b); // not swap
24+
System.out.println(a+" "+b);
25+
26+
final A vinay = new A("Vinay");
27+
vinay.name = "other name";
28+
29+
// when a non primitive is final,you cannot reassign it.
30+
// vinay = new A("new object");
31+
A obj;
32+
for (int i = 0; i < 100000000; i++) {
33+
obj = new A("Random name");
34+
}
35+
}
36+
static void swap(int a,int b){
37+
int temp = a;
38+
a = b;
39+
b = temp;
40+
41+
}
42+
// below still not swap
43+
// because intenal implementation of Integr is the using final .
44+
static void swap(Integer a,Integer b){
45+
Integer temp = a;
46+
a = b;
47+
b = temp;
48+
49+
}
50+
51+
}
52+
53+
54+
class A{
55+
// final int num;
56+
// final variable always initialize while declaring.
57+
58+
59+
// If not premitive data type then it will be change..
60+
61+
// final Student vinay = new Student(){
62+
// vinay.name = "new name";
63+
// vinay = other object ( we can not do it)
64+
// }
65+
66+
// In above example we can change the name;
67+
68+
69+
final int num = 10;
70+
String name;
71+
72+
public A(String name){
73+
// System.out.println("Object is created");
74+
this.name = name;
75+
}
76+
77+
@Override
78+
protected void finalize() throws Throwable {
79+
System.out.println("Object is destroyed");
80+
}
81+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class Main {
2+
Singleton obj1 = Singleton.getInstance();
3+
Singleton obj2 = Singleton.getInstance();
4+
Singleton obj3 = Singleton.getInstance();
5+
6+
// all 3 ref(All 3 object which we declare above obj1,obj2,obj3) variables are pointing to just one object
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// You only want one instance of that class.
2+
// Only one object you want to allow.
3+
4+
public class Singleton {
5+
// In this class we creat only one object.
6+
7+
// You should not allow anyone to creat constructor.
8+
9+
private Singleton(){
10+
11+
}
12+
private static Singleton instance;
13+
14+
public static Singleton getInstance(){
15+
// check whether 1 obj only is created or not
16+
if(instance == null){
17+
instance = new Singleton();
18+
}
19+
return instance;
20+
}
21+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Which propeties are not directly linked with object is known as static.
2+
3+
// common to all object of the class is known as static.
4+
5+
// It is accessed by class name as well as this keyword.
6+
7+
// common to all the object is known as static
8+
9+
// whenever you deal with static varibale whether you decaring or changeing or accesing--> then do not do it with reference varable (Object) it work but... convence is not ... RIGHT APPROACH IS use CLASS NAME ..
10+
11+
// static variable is not depednet on the object. It will work even if you do not creat object. --> Best example of this line is main method in java. where entire code is runing...
12+
13+
14+
// static variable or methods or block.. anthing are run on the compile time.
15+
16+
package staticExample;
17+
18+
public class Human {
19+
20+
int age;
21+
String name;
22+
int salary;
23+
boolean married;
24+
25+
// long population; // static
26+
static long population; // static
27+
28+
29+
public Human(int age, String name, int salary, boolean married) {
30+
this.age = age;
31+
this.name = name;
32+
this.salary = salary;
33+
this.married = married;
34+
35+
36+
// this.population += 1;
37+
Human.population += 1;
38+
}
39+
40+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package staticExample;
2+
3+
// outside classes can not be static.
4+
5+
// static public class InnerClasses {
6+
// class Test{
7+
8+
// }
9+
// }
10+
11+
public class InnerClasses {
12+
13+
// Without static it will give error.
14+
15+
// class Test {
16+
// String name;
17+
18+
// public Test(String name) {
19+
// this.name = name;
20+
// }
21+
// }
22+
static class Test {
23+
String name;
24+
25+
public Test(String name) {
26+
this.name = name;
27+
}
28+
}
29+
30+
public static void main(String[] args) {
31+
Test a = new Test("Vinay");
32+
Test b = new Test("Patel");
33+
34+
System.out.println(a);
35+
}
36+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Inside the static method you can not use any method without static .
2+
3+
package staticExample;
4+
5+
import java.util.GregorianCalendar;
6+
7+
import javax.crypto.spec.GCMParameterSpec;
8+
9+
public class Main {
10+
public static void main(String[] args) {
11+
// Human vinay = new Human(22, "Vinay Patel", 50000, false);
12+
// Human sonu = new Human(22, "Sonu Patel", 60000, true);
13+
// Human Airpt = new Human(22, "Sonu Patel", 60000, true);
14+
15+
// System.out.println(vinay.population);
16+
// System.out.println(sonu.population);
17+
// System.out.println(Airpt.population);
18+
19+
// System.out.println(Human.population);
20+
// System.out.println(Human.population);
21+
// System.out.println(Human.population);
22+
23+
// greeting(); //This is non-static
24+
25+
// fun();
26+
27+
// this is not dependent on objects.
28+
static void fun(){
29+
// greeting(); //You cant use this because it requires on instance(object).
30+
// but the function you are using it in does not instance.
31+
32+
// we can use it like below
33+
Main obj = new Main();
34+
obj.greeting();
35+
}
36+
}
37+
// we know that something which is not static, belongs to an object.
38+
void greeting(){
39+
// fun(); // we can use it.
40+
System.out.println("HEYY");
41+
}
42+
43+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Packages are nothing but like a folder.
2+
3+
we have different Packages that will use different code or logic we can use it using import.
4+
5+
We can import the things from other Packages and we have use it.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package staticExample;
2+
3+
// this is a demo to show initialisation of static variabkes
4+
5+
public class StaticBlock {
6+
static int a=4;
7+
static int b;
8+
9+
// will inly run once,when the first obj is created i.e when the class is loaded for the first time.
10+
11+
static{
12+
System.out.println("I am in static block");
13+
b = a*5;
14+
}
15+
public static void main(String[] args) {
16+
StaticBlock obj = new StaticBlock();
17+
System.out.println (StaticBlock.a+" "+StaticBlock.b);
18+
19+
StaticBlock.b += 3;
20+
StaticBlock obj2 = new StaticBlock();
21+
System.out.println (StaticBlock.a+" "+StaticBlock.b);
22+
23+
24+
}
25+
}

0 commit comments

Comments
 (0)