Você está na página 1de 36

BITS Pilani

BITS Pilani
Hyderabad Campus

Dr.Aruna Malapati Asst Professor Department of CSIS

BITS Pilani
Hyderabad Campus

OBJECT ORIENTED PROGRAMMING Classes and objects

Todays Agenda
Class

How to define a class? Members of class

Objects
Declaration Instantiation Initialization

Constructors
Unparametrized Constructor Parameterized Constructor Overloaded Constructor

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

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

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Spot the differences

Breed Age Size Color

Common Characteristics

Data Members identified

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Common Behavior

Eat Sleep Sit Run

Common Actions

Actions Identified

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Class Vs Objects

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

OO Programming Concepts
Data Member1 Class Construction Data Member2 Properties

Data Member3
Data Member4

Method1
Method2 Method3 Method4

Methods

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

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.

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Class and objects

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Class Declaration
class Circle { double radius = 1.0; double findArea() { return radius*radius*3.14159; } }

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

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.

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

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.

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Creating objects
objectName= new ClassName();
Example: myCircle = new Circle(); //Create (instantiate) the actual object

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Declaring objects / Creating objects in a single step


ClassName objectName= newClassName();

Example: Circle myCircle= new Circle();

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

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

Memory for objects


Box myBox1; Null Ox1000 OX3000 Width OX3000 OX1000 Height Depth Box myBox2 = new Box(); OX2000 OX2500 myBox1 = new Box(); OX2000 Width Height Depth

myBox1 and myBox2 is called a reference variable because it references the object myBox in memory.

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Copying variables of primitive data types and object types

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Garbage collection
After c1=c2, c1 object is known as garbage. Java runtime system detects garbage and automatically reclaims the space it occupies.

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Accessing objects
Referencing the objects data:

objectName.data
myCircle.radius Instance Variable

Referencing the objects method:

objectName.method
myCircle.findArea() Instance method
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

Instance variables and methods


Instance variables belong to a specific instance. Instance methods are invoked by an instance of the class.

myCircle.radius
myCircle.findArea()

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

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; } }

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Adding Methods to a class


class Box <modifiers> <return_type> name { ([parameter-list])[throws double width; <exception>] double height; { double depth; //body of the method void volume() } {

S.O.P(vol ume is + width *height*depth); }


CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Returning a value
class Box { double width; double height; double depth; double volume() { return width *height*depth; } }

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

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.

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

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.

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Types of Constructors
Unparametrized Constructor Parameterized Constructor Overloaded Constructor

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

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.

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

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()); } }

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Use 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.

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

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.

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Instance Variable Hiding


It is illegal in Java to declare two local variables with the same name inside the same or enclosing scopes. Interestingly, you can have local variables, including formal parameters to methods, which overlap with the names of the class' instance variables. // Use this to resolve name-space collisions. Box(double width, double height, double depth) { this.width = width; this.height = height; this.depth = depth; }
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

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.

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Você também pode gostar