21
BY: HUMA SAMIN Object Oriented Programming Concepts

OOP concepts

Embed Size (px)

DESCRIPTION

oops concept chapter

Citation preview

Page 1: OOP concepts

B Y :

H U M A S A M I N

Object Oriented Programming Concepts

Page 2: OOP concepts

Class and Object

Class is a user defined datatype.

The class definition provides a template or blueprint, which describes

the data (instance variables) contained within, and

the behavior (methods) common to all objects of a class.

Object is a variable of a class.

Page 3: OOP concepts

Syntax of class

class ClassName

{//data or instance variables

//behavior or methods

}

Page 4: OOP concepts

Data or Instance Variables

The data is contained in variables defined within the class

Often called instance variables, data members, properties or attributes.

The instance variables are variables of the primitive types or they can be reference variables of objects of other classes.

Page 5: OOP concepts

Example

class Student

{

//data members or instance variables

int rollno;

String name;

int semester;

int[] marks;

//behavior or methods

}

Page 6: OOP concepts

Behavior or Methods

The behavior is controlled by methods defined within the class.

Often called member methods or member functions.

Syntax:

returntype methodName(parameterlist)

{

//valid java statements

}

Page 7: OOP concepts

Example

class Student

{

//data members or instance variables

int rollno;

String name;

int semester;

//behavior or methods

void displayValues( )

{

System.out.println(rollno);

System.out.println(name);

System.out.println(semester);

}

}

Page 8: OOP concepts

Object

Object is a variable of a class.

Object is the implementation of class.

It is a software bundle of variables and methods.

It is also known as instance of a class.

The members of the class both data members and methods are accessed with the help of the object.

Page 9: OOP concepts

Declaration and Definition of Object

Syntax:

Declaration of object:

ClassName objectName;

Definition:

ObjectName=new ClassName( );

Shortcut:

ClassName objectName=new ClassName( );

Page 10: OOP concepts

Example

Objects are created in the main method or any other class.

public class StudentDriver

{

public static void main(String args[])

{

Student s;

s=new Student( );

}

}

Page 11: OOP concepts

Accessing Members of the class

The members of the class(both data members and methods) are accessed with the help of the object of the class.

Syntax:

objectName.instanceVariable=value;

OR

objectName.methodName();

Page 12: OOP concepts

Access Modifiers or Access Specifiers

Access Specifier

Class Package SubClass World

private Y N N N

package Y Y N N

protected Y Y Y N

public Y Y Y Y

Page 13: OOP concepts

Example

public class Student

{

//data members or instance variables

private int rollNo;

public int semester;

//behavior or methods are kept public

public void displayValues( )

{

System.out.println(rollno);

System.out.println(name);

System.out.println(semester);

}

}

Note: Data Members are kept private and methods are kept public

Page 14: OOP concepts

Example Continued..

public class StudentDriver

{

public static void main(String args[])

{

Student s;

s=new Student( );

s.semester=2;

s.rollNo=123; //Not Allowed as its private

s.displayValues( );

}

}

Page 15: OOP concepts

Accessing private members

To access the private members of the class, we have to provide getter and setter methods in the class.

The getters and setters have public access specifier.

If x & y are the instance variables then for setters, word “set” is used before the instance variable name like setX, setY.

For getters, word “get” is used before the instance variable name getX, getY.

Page 16: OOP concepts

Example

public class Student{

//data members or instance variablesprivate int rollNo;public int semester;

//behavior or methods are kept publicpublic void setrRollNo(int r) {

rollNo=r;}

public int getRollNo( ) {

return rollNo;}

public void displayValues( ){

System.out.println(rollNo);System.out.println(semester);

}}

Page 17: OOP concepts

Example cont..

public class StudentDriver{

public static void main(String args[]){

Student s;s=new Student( );

s.semester=2;System.out.println(s.semester);

s.setrollNo(123);System.out.println(s.getrollNo( ));

int r;r=s.getrollNo();System.out.println(r);

s.displayValues( );

}

}

Page 18: OOP concepts

Constructors

Constructor is a special kind of method of the class having the same name as that of the class and has a no return type.

It is called when an object of the class is going to be created.

The main purpose of writing constructor is initialization of instance variables.

Page 19: OOP concepts

Constructor Example

public class Student{

//data members or instance variablesprivate int rollNo;public int semester;

//behavior or methods are kept public//Constructorpublic Student( ){

rollNo=10;semester=2;

}

public setrollNo(int r) {

rollNo=r;}

public int getrollNo( ) {

return rollNo;}

public void displayValues( ){

System.out.println(rollNo);System.out.println(semester);

}}

Page 20: OOP concepts

Constructor

If you don’t provide constructor for class, the JVM will provide a default (zero argument) constructor and initialize the instance variables to default values.

Page 21: OOP concepts

Lab Work

Write a program to create a class named Circle having radius as a data member. The class should contain two methods to calculate the area and circumference of the circle.

You have to create two objects of the Circle class and display their area and circumference.