You are on page 1of 31

Lecture –Objects & Classes

Dr. Sapna Malik


Assistant Professor, MSIT
Objects & Classes

A class consists of variables called fields together with


functions called methods or member functions that act
on those fields.
General Structure of a Class

Members of a class are:

fields or variables
a class or object's state or attributes
methods
a class or object's behaviour
constructors
to make objects
each has access control
General Structure of a Class
Types of Members
fields or variables
instance unique values for each object
class uses the static modifier
same value for all objects
methods
instancework with both instance and
static variables
class uses static modifier
works with static variables
General Structure of a Class
Fields or Variables

 field or variable holds a single value


 is strongly typed: must be declared
 type specifies the kind of value and the variable's
behaviour
 int i = 123; // integer primitive
 Point pt = null; // object reference
 pt = new Point (x, y); // coordinates
pt has the value of the Point object's address, not the x, y
coordinates
Objects
In c++,we make object like this

B1
Box b1
Length

Height

Breath
Box *p=new Box();

Length

Height
p
Breath
Objects
In c++,we make object like this
Box b1 Length
Box *p=new Box(); Height
In java
Breath
Box b1; //only reference variable is created
Box b1=new Box ()
Objects in Java
In java
 Box b1; //only reference variable is created

Stack
 Box b1=new Box () Heap
B1 NULL

Length
Stack
Height
B1 2000
Breath
Classes and Objects
A class is a collection of fields (data) and methods (procedure
or function) that operate on that data.

Box

Length,Breadth,Height

setDimensions()
showDimensions()
Classes and Objects
 A class is a collection of fields (data) and methods (procedure or function) that
operate on that data.
 The basic syntax for a class definition:

class ClassName [extends


SuperClassName]
{
[fields declaration]
[methods declaration]
}

 Bare bone class – no fields, no methods

public class Box {


// my circle class
}
Classes and Objects
Example
class Box
{
private int length,height,breadth;
public void setDimention(int l,int h,int b)
{
length=l;
height=h;
breadth=b;
}
public void showDimention()
{
System.out.println("length= " + length + " height= " + height+ " breadth= " +
breadth);
}
Classes and Objects
Example-Box Class
public class Example{
public static void main(String[] args)
{
Box b1=new Box();
b1.setDimention(23,34,45);
b1.showDimention();
}
}

Output:
Length=23 Height=34 Breadth=45
Classes and Objects
Example-Box Class
Find the Output

public class Example{


public static void main(String[] args){
Box b1=new Box();
b1.setDimention(23,34,45);
b1.showDimention();
b1=new Box();
b1.showDimention();
}
}
Output: ?
Classes and Objects Example-Box Class
Box b1=new Box();
b1.setDimention(23,34,45);
b1.showDimention();
Heap 2000
b1=new Box();
b1.showDimention();
Length 23

Stack Height 34

3000 Breath 45
b1

Length 0

Height 0

Breath 0
Object Initialization

There are 3 ways to initialize object in Java.

By reference variable
By method
By constructor
Anonymous Object
Anonymous simply means nameless. An object which has no reference
is known as an anonymous object. It can be used at the time of object
creation only.

If you have to use an object only once, an anonymous object is a good
approach.
For example:

new Calculation();//anonymous object

Calling method through an anonymous object

new Calculation().fact(5);
Anonymous Object
class Calculation{
void fact(int n){
int fact=1;
for(int i=1;i<=n;i++){
fact=fact*i;
}
System.out.println("factorial is "+fact);
}
public static void main(String args[]){
new Calculation().fact(5);//calling method with anonymous object
}
}
Output: Factorial is 120
Constructors
constructor: Initializes the state of new objects.

public type(parameters) {
statements;
}
a special kind of method to create an object of class.
no return type is specified, it implicitly "returns" the new object being created
default constructor is ClassName()
Can not use any modifier.
If a class has no constructor, Java gives it a default constructor with no parameters that sets
all fields to 0.
Can not be overridden but can be overloaded : same name with different parameter lists.
If the parameterized constructor is defined in the class but the default constructer is
called ,then the compile time error occurs.
Java provides a Constructor class which can be used to get the internal information of a
constructor in the class. It is found in the java.lang.reflect package.
Classes and Objects
Example
class Box{
private int length,height,breadth;
Box (int l,int h,int b){
length=l;
height=h;
breadth=b;
}
public void showDimention(){
System.out.println("length= " + length + " height= " + height+ " breadth= " +
breadth);
}
Classes and Objects
Example-Box Class
public class Example
{
public static void main(String[] args)
{
Box b1=new Box(23,34,45);
b1.showDimention();
b1=new Box(); // Compile time error
}
}

Output:
Length=23 Height=34 Breadth=45
this keyword
this is a reference variable that refers to the current object.

Heap 2000

Length 23
b1 2000
Height 34

Breath 45

this 2000
this keyword Usage
1. this can be used to refer current class instance
variable.
2. this can be used to invoke current class method
(implicitly)
3. this() can be used to invoke current class constructor.
4. this can be passed as an argument in the method call.
5. this can be passed as argument in the constructor
call.
6. this can be used to return the current class instance
from the method.
Classes and Objects
Example-Box
1. this can be used to refer current class instance
variable.
class Box
{
private int l,h,b;
Box (int l,int h,int b)
{
this.l=l;
this.h=h;
this.b=b;
}
public void showDimention()
{
System.out.println("length= " + length + " height= " + height+ " breadth= " +
breadth);
Classes and Objects
Example-Box Class
public class Example
{
public static void main(String[] args)
{
Box b1=new Box(23,34,45);
b1.showDimention();

}
}

Output:
Length=23 Height=34 Breadth=45
this Keyword Usage
2. this can be used to invoke current class method (implicitly).
class A{  
void m(){System.out.println("hello m");}  
void n(){  
System.out.println("hello n");  
//m();//same as this.m()  
this.m();  
}  }  
class TestThis4{  
public static void main(String args[]){  
A a=new A();  
a.n();  
}}  
Output: hello n
hello m
this Keyword Usage
3. this() can be used to invoke current class
constructor.
class A{
A(){System.out.println("hello a");}
A(int x){
this();
System.out.println(x);
} }
class TestThis5{
public static void main(String args[]){
A a=new A(10);
}}
Output: hello a
10
this Keyword Usage
 this() can be used to invoke current class constructor.
 Call to this() must be the first statement in constructor.
class A{
A(){
this(5);
System.out.println("hello a");
this(5); //error
}
A(int x){
System.out.println(x); }}
class TestThis6{
public static void main(String args[]){
A a=new A(); }}
Output: 5
hello a
this Keyword Usage
4. this can be passed as an argument in the method
call.
class S2{
void m(S2 obj){
System.out.println("method is invoked");
}
void p(){
m(this);
}
public static void main(String args[]){
S2 s1 = new S2();
s1.p();
} }
Output: method is invoked
this Keyword Usage
5. this: to pass as argument in the constructor call

class A {
B obj;
A (B obj) {
this.obj=obj;
}
void display(){
System.out.println(obj.data);//using data member of B class
}
}
this Keyword Usage
5. this: to pass as argument in the constructor call
class B {
int data=10;
B (){
A a=new A(this);
a.display();
}
public static void main(String args[]){
B b=new B ();
}
}
Output: 10
this Keyword Usage
6. this keyword can be used to return current class
instance
class A{
A getA(){
return this;
}
void msg(){System.out.println("Hello java");}
}
class Test1{
public static void main(String args[]){
new A().getA().msg();
}
}
Output: Hello java

You might also like