Skip to content

Commit fa600c8

Browse files
class || obj || this || new .notes added
1 parent 3179790 commit fa600c8

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

oops/01-Inrtoduction/Class.text

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
A class is a template for an object, and an object is an instance of a class.
2+
A class creates a new data type that can be used to create objects.
3+
4+
When you declare an object of a class, you are creating an instance of that class.
5+
Thus, a class is a logical construct. An object has physical reality. (That is, an object occupies space in memory.)
6+
7+
Objects are characterized by three essential properties: state, identity, and behavior.
8+
The state of an object is a value from its data type. The identity of an object distinguishes one object from another.
9+
It is useful to think of an object’s identity as the place where its value is stored in memory.
10+
The behavior of an object is the effect of data-type operations.
11+
12+
13+
In object-oriented programming, the dot operator is used to link the name of an object with the name of an instance variable or method of that object.
14+
It is also commonly referred to as the dot notation or dot syntax.
15+
16+
17+
18+
19+
The 'new' keyword dynamically allocates(that is, allocates at run time)memory for an object & returns a reference to it.
20+
This reference is, more or less, the address in memory of the object allocated by new.
21+
This reference is then stored in the variable.
22+
Thus, in Java, all class objects must be dynamically allocated.
23+
24+
25+
26+
27+
Box mybox; // declare reference to object
28+
mybox = new Box(); // allocate a Box object
29+
The first line declares mybox as a reference to an object of type Box. At this point, mybox does not yet refer to an
30+
actual object. The next line allocates an object and assigns a reference to it to mybox. After the second line executes,
31+
you can use mybox as if it were a Box object. But in reality, mybox simply holds, in essence, the memory address of the
32+
actual Box object.
33+
The key to Java’s safety is that you cannot manipulate references as you can actual pointers.
34+
Thus, you cannot cause an object reference to point to an arbitrary memory location or manipulate it like an integer.
35+
36+
A Closer Look at new:
37+
classname class-var = new classname ( );
38+
Here, class-var is a variable of the class type being created. The classname is the name of the class that is being
39+
instantiated. The class name followed by parentheses specifies the constructor for the class. A constructor defines
40+
what occurs when an object of a class is created.
41+
42+
You might be wondering why you do not need to use new for such things as integers or characters.
43+
The answer is that Java’s primitive types are not implemented as objects.
44+
Rather, they are implemented as “normal” variables.
45+
This is done in the interest of efficiency.
46+
47+
It is important to understand that new allocates memory for an object during run time.
48+
49+
Box b1 = new Box();
50+
Box b2 = b1;
51+
b1 and b2 will both refer to the same object. The assignment of b1 to b2 did not allocate any memory or copy any part
52+
of the original object. It simply makes b2 refer to the same object as does b1. Thus, any changes made to the object
53+
through b2 will affect the object to which b1 is referring, since they are the same object.
54+
When you assign one object reference variable to another object reference variable, you are not creating a copy of the
55+
object, you are only making a copy of the reference.
56+
57+
int square(int i){
58+
return i * i;
59+
}
60+
A parameter is a variable defined by a method that receives a value when the method is called. For example,
61+
in square( int i), i is a parameter. An argument is a value that is passed to a method when it is invoked.
62+
For example, square(100) passes 100 as an argument. Inside square( ), the parameter i receives that value.
63+
64+
NOTE:
65+
Bus bus = new Bus();
66+
lhs(reference i.e. bus) is looked by compiler & rhs (object i.e. new Bus()) is looked by jvm
1.32 KB
Binary file not shown.

oops/01-Inrtoduction/Class01.class

928 Bytes
Binary file not shown.

oops/01-Inrtoduction/Class01.java

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import java.util.*;
2+
3+
public class Class01 {
4+
public static void main(String[] args) {
5+
6+
// data of 5 students: {roll no, name, marks}
7+
int[] rno = new int[5];
8+
String[] name = new String[5];
9+
float[] marks = new float[5];
10+
11+
// instaed of this just make Student class own data type or you can say templet
12+
// for student
13+
Student[] students = new Student[5]; // student type array whre student has all propties name , rno , marks for
14+
// state and function for state change
15+
16+
// just declaring
17+
// Student devesh; // memory created at stack not in heap
18+
// devesh = new Student(); //memory created at heap using new Keyword
19+
20+
// ! we can use access inatnce variables and functionusing . dot operator
21+
// devesh.changeName("Shoe lover");
22+
// devesh.greeting();
23+
24+
Student devesh = new Student(15, "devesh", 85.4f);
25+
26+
System.out.println(devesh.rno);
27+
System.out.println(devesh.name);
28+
System.out.println(devesh.marks);
29+
30+
// this is how we can use other student data with new student
31+
Student random = new Student(devesh); // (Student other) will acess in calss
32+
System.out.println(random.name);
33+
System.out.println(random.rno);
34+
System.out.println(random.marks);
35+
36+
// call one constructor to other
37+
Student one = new Student();
38+
System.out.println(one.name);
39+
System.out.println(one.rno);
40+
System.out.println(one.marks);
41+
42+
// ! out put for this =>
43+
// default person
44+
// 13
45+
// 100.0
46+
Student two = one; // two is pointing to the same obejct as like one
47+
48+
}
49+
50+
// create a class
51+
// for every single student
52+
public static class Student {
53+
int rno;
54+
String name;
55+
float marks = 90;
56+
57+
// this keyword => this keyword refering to intsance of Student class or you can
58+
// say its behave like refernce variable of object that is created using Student
59+
// class. in our case devesh is a student and thsi referes to devesh
60+
void greeting() {
61+
System.out.println("Hello! My name is " + this.name);
62+
}
63+
64+
void changeName(String name) {
65+
this.name = name;
66+
}
67+
68+
// Student arpit = new Student(17, "Arpit", 89.7f);
69+
// here, this will be replaced with arpit
70+
Student(int rno, String name, float marks) {
71+
this.rno = rno;
72+
this.name = name;
73+
this.marks = marks;
74+
75+
}
76+
77+
// this how we can use other object data with new object. other is object passed
78+
// witj constructor while called
79+
Student(Student other) {
80+
this.name = other.name;
81+
this.rno = other.rno;
82+
this.marks = other.marks;
83+
}
84+
85+
// default constructor
86+
// this is how you call a constructor from another constructor
87+
Student() {
88+
// internally: new Student (13, "default person", 100.0f);
89+
// here this is pointing a constructor who takes three paramtere as args
90+
this(13, "default person", 100.0f); // these are the deafult value passed from here
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)