Classes & Objects
Classes & Objects
Module 2
Introducing Classes Objects & Methods
Course Faculty
Dr.Chandrika.J
Professor & Head
ISE
Module 2 Syllabus
• Introducing Classes, Objects and Methods: Class Fundamentals,
Declaring Objects, Object Reference Variables, Methods,
Constructors, The this keyword, Garbage collection, Overloading
Methods and constructors, Argument Passing, Returning Objects,
Recursion, Access Control, Nested and Inner Classes.
• Inheritance, Packages and Interfaces: Inheritance Basics, Using Super,
Multilevel Hierarchy, When Constructors are called, Method
Overriding, Abstract Classes. Packages, Access Protection, Importing
Packages, Interfaces.
Class Fundamentals(23-11-2021)
• A class is a template that defines the form of an object.
• It specifies both the data and the code that will operate on that data.
• The methods and variables that constitute a class are called members of the
class. The data members are also referred to as instance variables.
class Boxdemo1
{
public static void main(String args[])
{
Box mybox = new Box();
double vol;
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
vol = mybox.width * mybox.height * mybox.depth;
System.out.println(“Volume is “ + vol);
}
}
Number of classes?
Output : Volume is 3000.0 Number of instance variables?
Number of objects/instances?
Identify type conversion
Identify type casting
Observe Indentation
Example Program –Declaring 2 objects
class Box {
double width; double height; double depth;
}
class Boxdemo2{
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox 2= new Box();
double vol;
}
}
Output : Volume is 3000.0
Volume is 162.0
Declaring Objects
mybox1.volume();
mybox2.volume();
}
}
Returning a Value
class Box {
double width; double height; double depth;
double volume() {
return width * height * depth;
}
}
class Boxdemo4 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
vol = mybox1.volume();
System.out.println(“Volume is “ + vol);
mybox2.volume();
System.out.println(“Volume is “ + vol);
}
}
Adding a method that takes Parameters
class Box {
double width; double height; double depth;
double volume() {
return width * height * depth;
}
void setdim(double w, double h, double d) { // parameterized method
width = w; height = h; depth = d;
}
}
class Boxdemo5{
public static void main(String args[ ]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
vol = mybox1.volume();
System.out.println(“Volume is “ + vol);
vol=mybox2.volume();
System.out.println(“Volume is “ + vol);
}
}
Constructors
class Box {
double width; double height; double depth;
//Constructor
Box() {
width = 10;
height = 10;
depth = 10;
}
double volume() {
return width * height * depth;
}
}
class Boxdemo6{
public static void main(String args[ ]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
vol = mybox1.volume();
System.out.println(“Volume is “ + vol);
vol=mybox2.volume();
System.out.println(“Volume is “ + vol);
}
}
Parameterized Constructors
class Box {
double width; double height; double depth;
//Constructor
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
double volume() {
return width * height * depth;
}
}
class Boxdemo7{
public static void main(String args[ ]) {
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box(3, 6, 9);
double vol;
vol = mybox1.volume();
System.out.println(“Volume is “ + vol);
vol=mybox2.volume();
System.out.println(“Volume is “ + vol);
}
}
The this keyword (25-11-2021)
• 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 an object on which the method was invoked.
• E.g., Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
}
• Useful when a local variable has the same name as an instance variable. The local
variable hides the instance variable.
• Used to solve any namespace collisions that might occur between instance variables
and local variables.
What is a namespace???
• The names of those classes will not collide with identically named
classes in different packages.
Garbage Collection(GC)
• In C++ memory released manually by use of DELETE operator.
• E.g., object is holding some non-java resource such as file handle or character
font.
class Overeloadcons{
public static void main(String args[ ]) {
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
vol = mybox1.volume();
System.out.println(“Volume of mybox1 “ + vol);
vol=mybox2.volume();
System.out.println(“Volume of mybox2“ + vol);
vol = mycube.volume();
System.out.println(“Volume of mycube“ + vol);
} //end main
}// end Overloadcons
Using Object as Parameters
class Box { double width; double height; double depth;
//Pass object to Constructor
Box(Box ob) { //Pass object to constructor
width = ob.width; height = ob.height; depth = ob.depth; }
//Constructor - 2
Box(double w, double h, double d) {
width = w; height = h; depth = d; }
// Constructor – 3
Box() {
width= -1; height = -1; depth = -1;}
// Constructor – 4
Box(double len) {width = height = depth = len; } //cube created
double volume() {
return width * height * depth; } }//end Box
class Overeloadcons2{
public static void main(String args[ ]) {
Box mybox1 = new Box(10, 20, 15); Box mybox2 = new Box();
Box mycube = new Box(7); Box myclone = new Box(mybox1);
double vol;
vol = mybox1.volume();
System.out.println(“Volume of mybox1 = “ + vol);
vol=mybox2.volume();
System.out.println(“Volume of mybox2 = “ + vol);
vol = mycube.volume();
System.out.println(“Volume of mycube = “ + vol);
vol = myclone.volume();
System.out.println(“Volume of clone = “ + vol); } }
TRY THIS
Write a java program to compute area of a rectangle using parameterized constructor
class rect
{ class rectdemo
int l,b,area; {
rect(int len,int br) public static void main(String[]
{ args)
l=len; {
b=br; rect r1=new rect(12,20);
} r1.rectarea();
void rectarea()
{ }
area=l*b; }
System.out.println("area of rectangle="+ area);
}
}
A closer look at Argument Passing
• To pass an argument to a subroutine – call-by-value, call-by-reference
• Call-by-value - copies the value of an argument into the formal parameter
of the subroutine.
• Changes made to the parameter of the subroutine have no effect on the
argument.
• Call-by-reference – A reference to an argument is passed to the parameter.
• Changes made to the parameter will effect the argument used to call
the subroutine.
• In Java, passing a primitive type to a method, is passed by value.
• Objects are passed by Reference.
Objects are passed by reference
Class Test {
int a,b;
Test(int i, int j) { a = i; b = j; }
//Pass an object
Void arith(Test p) {
p.a *= 2; p.b /= 2; }
}
Class Callbyref {
p s v main(…) {
Test ob = new Test(15, 20);
s.o.println(“ ob.a and ob.b before call: “ + ob.a + “ “ + ob.b);
ob.arith(ob);
s.o.println(“ ob.a and ob.b after call: “ + ob.a + “ “ + ob.b);
}
}
Returning Objects 26-11-2021
A method can return any type of
class Retob{
data, including class types.
public static void main(String args[]){
// Returning an object.
Test ob1 = new Test(2);
class Test {
Test ob2;
int a;
Test(int i) ob2 = ob1.incrByTen( );
{a = i;} System.out.println(“ ob1.a : “ + ob1.a);
Test incrByTen( ){ System.out.println(“ ob2.a : “ + ob2.a);
} }
Introducing Access Control
• Encapsulation provides access control. Through encapsulation it is possible to control what
parts of a program can access the members of a class.
• Can prevent misuse.
• How a member can be accessed is determined by access specifier that modifies the
declaration.
• Java’s access specifier’s are public, private and protected.
• Protected - applies only when inheritance is involved.
• Public – the specified member can be accessed by any other code.
• Private - the specified member can only be accessed by other members of its class.
• main() is public – it is called by code outside the program, the Java run-time system.
• When no access specifier is used, then by default the member of a class is public within its
own package, but cannot be accessed outside of its package.
Example Program to differentiate between public and private (1-12-21)
Class Test { Class AccessTest {
int a; public static void main(String args[]) {
public int b; Test ob = new Test();
private int c; //These are OK, a and b may be accessed directly
ob.a = 10;
//methods to access c ob.b = 20;
void setc(int i) { // This is not OK and will cause an error
c = i; // ob.c = 100;
} // you must access c through its methods
ob.setc(100); //OK
int getc(){ //get c’s value System.out.println(“ a, b and c : “ + ob.a + “ “
return c; + ob.b + “ “ + ob.getc( ) );
} }
}
}
Understanding static
It is possible to define a class member that will be used independently of
any object of that class.
To create such a member, precede its declaration with the keyword static.
main( ) is declared as static because it must be called by the JVM when your program
begins.
Outside the class, to use a static member, you need only specify the name of its class
followed by the dot operator.
For example, if you want to assign the value 10 to a static variable called count that is
part of the Timer class, use this line:
Timer.count = 10
EXAMPLE PROGRAM
Understanding static
static is applicable for the following:
• blocks
• variables
• methods
• nested classes
• To create a static member(block,variable,method,nested class), precede its
declaration with the keyword static.
• When a member is declared static, it can be accessed before any objects of
its class are created, and without reference to any object.
Static variables
• When a variable is declared as static, then a single copy of variable is created
and shared among all objects at class level.
class OuterClass
{
// ...
class NestedClass
{
// ...
}}
• non static nested class nested class is also called an inner class.
• an inner class that does not have a name is called an anonymous inner class.
// Driver class
public class InnerClassDemo
{
public static void main(String[] args)
{
OuterClass.InnerClass innerObject = new OuterClass().new InnerClass();
innerObject.display();