Escolar Documentos
Profissional Documentos
Cultura Documentos
BITS Pilani
Hyderabad Campus
BITS Pilani
Hyderabad Campus
Todays Agenda
Class
Objects
Declaration Instantiation Initialization
Constructors
Unparametrized Constructor Parameterized Constructor Overloaded Constructor
OO Programming Concepts
Classes are blueprints or structures for defining objects Each object is an instance of class. In Java, each object has data which are used to describe properties, and methods which are used to describe behaviors. Each Java file is a class.
Class Object
Common Characteristics
Common Behavior
Common Actions
Actions Identified
Class Vs Objects
OO Programming Concepts
Data Member1 Class Construction Data Member2 Properties
Data Member3
Data Member4
Method1
Method2 Method3 Method4
Methods
DEFINING A CLASS
<scope> [<final>/<abstract>] [static>] class [extends <classname>] <classname> [implements <interfacename 1> .<interface n>]
{
Member Variable declarations /Instance field declarations; Member Method Definitions; }
Class Body
<scope> : 1. package private or public for outer classes 2. private , public , protected, package private for inner classes <final> : class definition is final and can not be extended by sub classes. final class can not have sub classes <static> : static keyword can only be applied for inner classes. Outer classes can not be static. <abstract> : abstract keyword specifies that class is abstract.
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
Access Specifiers
The four access levels are: Visible to the package. the default. No modifiers are needed. Visible to the class only (private). Visible to the world (public). Visible to the package and all subclasses (protected).
The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them.
Class Declaration
class Circle { double radius = 1.0; double findArea() { return radius*radius*3.14159; } }
Creating an Object
In java the new key word is used to create new objects. There are three steps when creating an object from a class: Declaration : A variable declaration with a variable name with an object type. Instantiation : The 'new' key word is used to create the object. Initialization : The 'new' keyword is followed by a call o a constructor. This call initializes the new object.
Declaring objects
ClassName objectName; Example: Circle myCircle; // Declare an object reference variable
null
myCircle
Note: Any attempt to use myCircle at this point will cause compile time error.
Creating objects
objectName= new ClassName();
Example: myCircle = new Circle(); //Create (instantiate) the actual object
Another example
class Box { double width; double height; double depth; } class BoxDemo { 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); } }
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
myBox1 and myBox2 is called a reference variable because it references the object myBox in memory.
Garbage collection
After c1=c2, c1 object is known as garbage. Java runtime system detects garbage and automatically reclaims the space it occupies.
Accessing objects
Referencing the objects data:
objectName.data
myCircle.radius Instance Variable
objectName.method
myCircle.findArea() Instance method
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
myCircle.radius
myCircle.findArea()
Using objects
//TestCircle.java: Demonstrate creating and using an object public classTestCircle { // Main method public static void main(String[]args) { Circle myCircle= new Circle(); // Create a Circle object System.out.println("The area of the circle of radius + myCircle.radius + " is " +myCircle.findArea()); Circle lynnCircle=new Circle(); System.out.println(lynnCircle.radius); System.out.println(lynnCircle.findArea() ); Circle c1, c2, c3,c4,c5; c1=new Circle(); c2=new Circle(); c3=new Circle(); c4=new Circle(); c5=new Circle(); System.out.println( c1.radius+c2.radius +c3.radius+c4.radius +c5.radius); } }
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
class Circle { double radius=1.0; // Find area of this circle public doublefindArea() { return radius*radius*3.14159; } }
Returning a value
class Box { double width; double height; double depth; double volume() { return width *height*depth; } }
Passing parameters
class Box Class BoxDemo { { double width; Box mybox = new Box(); double height; mybox.setDim(10,20,15); double depth; } void volume() { return width *height*depth; } void setDim(double w, double h, double d) { width = w; height = h; depth=d; } } CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
Constructor
A constructor is a special method that is used to initialize a newly created object and is called just after the memory is allocated for the object. It can be used to initialize the objects ,to required ,or default values at the time of object creation. It is not mandatory for the coder to write a constructor for the class.
Constructor
If no user defined constructor is provided for a class, compiler initializes member variables to its default values.
numeric data types are set to 0 char data types are set to null character reference variables are set to null
Code written inside constructor method is automatically executed for every object creation of that class. In order to create a Constructor observe the following rules.
It has the same name as the class. It should not return a value not even void.
Types of Constructors
Unparametrized Constructor Parameterized Constructor Overloaded Constructor
Unparametrized Constructor
If a class does not supply any constructor then JRE supplies a default constructor with no parameters. Class can have its own constructor of any types. If a class supplies its own constructor then default constructor becomes hidden.
A parameterized constructor
class Box { double width; double height; double depth; double volume() { return (width *height*depth); } Box(double w,double h,double d) { System.out.println("object created with parameters passed"); width=w; height=h; depth=d; } } class WithParamCons { public static void main(String args[]) { Box mybox1 = new Box(10,20,30); System.out.println("volume is " + mybox.volume()); } }
Example
// A redundant use of this. Box(double w, double h, double d) { width = w; height = h; depth = d; } Inside Box( ), this will always refer to the invoking object.
Summary
A Class is a blue print that has common properties of objects. A class can have multiple instances created called objects. A class has attributes and methods. An object name is a reference to memory allocated to the object. A class can have constructors that initializes the state of the object when it is created.