Object and Class
Object and Class
Object: It is a basic unit of Object Oriented Programming and represents the real life entities. A typical
Java program creates many objects, interact by invoking methods.
An object consists of :
Class :
A class is a user defined blueprint or prototype from which objects are created. It represents the set of
properties or methods that are common to all objects of one type. A class in Java is a blueprint from
which an object is created. It is a logical entity that helps in defining the behavior and properties of an
object. In general, class declarations can include these components, in order:
1. Private: The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.
2. Default: The access level of a default modifier is only within the package. It cannot
be accessed from outside the package. If you do not specify any access level, it will
be the default.
3. Protected: The access level of a protected modifier is within the package and
outside the package through child class. If you do not make the child class, it cannot
be accessed from outside the package.
4. Public: The access level of a public modifier is everywhere. It can be accessed from
within the class, outside the class, within the package and outside the package.
Class name: The name should begin with a initial letter (capitalized by convention).
Syntax of a Class
There are three simple steps to create a Java object which are listed below:
Declaration − This is the very first step of object creation. In this step, you need to declare a
variable with the class name as the data type.
Syntax:
classname objectname;
Instantiation − Next step is the instantiation where you need to use the ‘new’ keyword to create
the object.
Syntax:
classname objectname=new classname();
Initialization − Finally in the third step, you need to initialize the object by calling the class
constructor.
Syntax
classname objectname=new classname(values);
Using factory or newinstance methods: Java Class.newInstance() is the method of
Class class. The Class class belongs to java.lang package. It creates a new instance
of the class represented by this Class object. It returns the newly created instance of
the class.
Variables
Local variables − Variables defined inside methods, constructors or blocks are called local
variables. The variable will be declared and initialized within the method and the variable will be
destroyed when the method has completed.
Instance variables − Instance variables are variables within a class but outside any method.
These variables are initialized when the class is instantiated. Instance variables can be accessed
from inside any method, constructor or blocks of that particular class.
Class variables − Class variables are variables declared within a class, outside any method, with
the static keyword.
Methods
A method is a collection of statements that perform some specific task and return the result to the
caller. A method can perform some specific task without returning anything. Methods allow us to reuse
the code without retyping the code. In Java, every method must be part of some class which is different
from languages like C, C++, and Python.
Methods are time savers and help us to reuse the code without retyping the code.
Syntax of a method:
modifier returntype methodname(parameter list){
Body of the method
return statement;
}
Example
// Program to illustrate methodsin java
class Addition {
int sum = 0;
public int addTwoInt(int a, int b){
// adding two integer value.
sum = a + b;
//returning summation of two values.
return sum;
}
}
public class Additiondemo {
public static void main (String[] args) {
// creating an instance of Addition class
Addition add = new Addition();
// calling addTwoInt() method to add two integer using instance created
// in above step.
int s = add.addTwoInt(1,2);
System.out.println("Sum of two integer values :"+ s);
}
}
There are a few important things to understand about returning the values
The type of data returned by a method must be compatible with the return type specified by the
method. For instance, if the return type of some method is boolean, we can not return an
integer.
The variable receiving the value returned by a method must also be compatible with the return
type specified for the method.
The parameters can be passed in a sequence and they must be accepted by the method in the
same sequence.
this keyword
Keyword 'this' in Java is a reference variable that refers to the current object. "this" is a reference to the
current object, whose method/variable is being called upon. You can use "this" keyword to avoid
naming conflicts in the method/constructor of your instance/object.
Syntax
this.variablename/methodname
Constructors
Constructors are used to initialize the object’s state. Like methods, a constructor also contains collection
of statements(i.e. instructions) that are executed at time of Object creation. A Java constructor is special
method that is called when an object is instantiated. In other words, when you use the new keyword.
The purpose of a Java constructor is to initializes the newly created object before it is used. The name of
the constructor should same as the name of the class.
Syntax
Accessmodifier class classname{
Accessmodifier classname(parameter list) {
}
}
Example
Write a Java program which accepts students name, rollno, and marks of 3 subjects. Write appropriate
constructor for the student which assigns values to the members. Use appropriate getters and setters
for the variables. Write a test application to display the details of the student with their total and
average marks.
class Student{
String name, rollno;
int marks1,marks2,marks3;
Student(String n,String r,int m1,int m2,int m3){
this.name=n;
this.rollno=r;
this.marks1=m1;
this.marks2=m2;
this.marks2=m2;
}
void setname(String n){
this.name=n;
}
String getname(){
return name;
}
void setrollno(String r){
this.rollno=r;
}
String getrollno(){
return rollno;
}
void setmarks1(int m){
this.marks1=m;
}
int getmarks1(){
return marks1;
}
void setmarks2(int m){
this.marks2=m;
}
int getmarks2(){
return marks2;
}
void setmarks3(int m){
this.marks3=m;
}
int getmarks3(){
return marks3;
}
void display(){
System.out.println("Name "+this.getname()+"\nRollno "+this.getrollno()+"\nMarks :
"+this.getmarks1()+" "+this.getmarks2()+" "+this.getmarks3());
}
}
public class Studentdemo {
public static void main(String args[]) {
Student s=new Student("Amar","MY.18001",45,60,87);
s.display();
int total=s.getmarks1()+s.getmarks2()+s.getmarks3();
float average=(float)total/3;
System.out.println("Total marks " + total+"\nAverage "+average);
}
}