You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Class defines a new data type. Once defined, this new type can be used to create objects of that type. Thus, a class is a template for an object, and an object is an instance of a class.
When you define a class, you declare its exact form and nature. You do this by specifying the data that it contains and the code that operates on that data. While very simple classes may contain only code or only data, most real-world classes contain both.
The data, or variables, defined within a class are called instance variables. The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class.
Class is just a design(architecture) of a real world entity. It occupies no memory.
For a better understanding, refer to this article.
Object :-
Class declaration only creates a template. It does not create an actual object.
Objects are the variable of the class. Thus, it will have “physical” reality(memory).
Each time you create an instance of a class, you are creating an object that contains its own copy of each instance variable defined by the class.
In general, you use the dot operator to access both the instance variables and the methods within an object.
For a better understanding about method and dot operator, refer to this article.
// This program uses a parameterized method.classBox {
doublewidth;
doubleheight;
doubledepth;
// compute and return volumedoublevolume() {
returnwidth * height * depth;
}
// sets dimensions of boxvoidsetDim(doublew, doubleh, doubled) {
width = w;
height = h;
depth = d;
}
}
classBoxDemo {
publicstaticvoidmain(Stringargs[]) {
Boxmybox1 = newBox();
Boxmybox2 = newBox();
doublevol;
// initialize each boxmybox1.setDim(10, 20, 15);
mybox2.setDim(3, 6, 9);
// get volume of first boxvol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second boxvol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
Each object has its own copies of the instance variables. Changes to the instance variables of one object have no effect on the instance variables of another.
Obtaining objects of a class is a two-step process.
First, you must declare a variable of the class type. This variable does not define an object. Instead, it is simply a variable that can refer to an object.(Reference Variable)
Second, you must acquire an actual, physical copy of the object and assign it to that variable. You can do this using the new operator. The new operator dynamically allocates (that is, allocates at run time) memory for an object and returns a reference to it. This reference is, more or less, the address in memory of the object allocated by new. This reference is then stored in the variable.
Constructor :-
A constructor initializes an object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method.
The constructor is automatically called when the object is created, before the new operator completes.
/* Here, Box uses a parameterized constructor to initialize the dimensions of a box.*/classBox {
doublewidth;
doubleheight;
doubledepth;
// This is the constructor for Box.Box(doublew, doubleh, doubled) {
width = w;
height = h;
depth = d;
}
// compute and return volumedoublevolume() {
System.out.println(this.width);
returnwidth * height * depth;
}
}
classBoxDemo {
publicstaticvoidmain(Stringargs[]) {
// declare, allocate, and initialize Box objectsBoxmybox1 = newBox(10, 20, 15);
// Box b1 = new Box(); This will cause error because of no default constructor.Boxmybox2 = newBox(3, 6, 9);
doublevol;
// get volume of first boxvol = mybox1.volume();
System.out.println("Volume is " + vol);
// get volume of second boxvol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
this Keyword :-
Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the this keyword. this can be used inside any method to refer to the current object.
This is always a reference to the object on which the method was invoked. You can use this anywhere a reference to an object of the current class’ type is permitted.
// Use this to resolve name-space collisions.Box(doublewidth, doubleheight, doubledepth) {
this.width = width;
this.height = height;
this.depth = depth;
}
Further readings (topics not covered in 1st class)
Overloading
In Java, it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading.
Method overloading is one of the ways that Java supports polymorphism.
When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call.
// Automatic type conversions apply to overloading.classOverloadDemo {
voidtest() {
System.out.println("No parameters");
}
// Overload test for two integer parameters.voidtest(inta, intb) {
System.out.println("a and b: " + a + " " + b);
}
// overload test for a double parameter and return typevoidtest(doublea) {
System.out.println("Inside test(double) a: " + a);
}
}
classOverload {
publicstaticvoidmain(Stringargs[]) {
OverloadDemoob = newOverloadDemo();
inti = 88;
ob.test();
ob.test(10, 20);
ob.test(i); // this will invoke test(double)ob.test(123.2); // this will invoke test(double)
}
}
Access Control
You can control what parts of a program can access the members of a class. By controlling access, you can prevent misuse.
When a member of a class is modified by public, then that member can be accessed by any other code.
When a member of a class is specified as private, then that member can only be accessed by other members of its class.
/* This program demonstrates the difference between public and private.*/classTest {
inta; // default accesspublicintb; // public accessprivateintc; // private access// methods to access cvoidsetc(inti) { // set c's valuec = i;
}
intgetc() { // get c's valuereturnc;
}
}
classAccessTest {
publicstaticvoidmain(Stringargs[]) {
Testob = newTest();
// These are OK, a and b may be accessed directlyob.a = 10;
ob.b = 20;
// This is not OK and will cause an error// ob.c = 100; // Error!// You must access c through its methodsob.setc(100); // OKSystem.out.println("a, b, and c: " + ob.a + " " +
ob.b + " " + ob.getc());
}
}