Object Oriented Programming - Java Programming Course - Chapter 2

Object Oriented Programming

Object:

·       The object oriented programming involves programming by using objects.

·       An object represents an entity in the real world that can be distinctly identified.

Ex: student,fan,light,circle,etc.,

·       An object has unique identity: State and behavior

·       State: The state of an object is nothing but the properties or attributes of the object which are represented by data fields or members.

Ex: The circle object has data fields like radius , area ,perimeter etc.,

·       Behavior: The behavior of an object is nothing but the actions what we want to do on the object. The behavior is represented by methods. Ex: Calculate Area(),Calculate Perimeter().

Class :

·       A class is a template blueprint which represents the state & behavior of objects.

·       Program to create a class Circle with radius & methods calArea() & calPerimeter().

class Circle

{

double radius; //here radius is called data field

double calArea()  //calArea() is a method

{

double area;

area=radius*radius*Math.PI;

return area;

}

double calPerimeter()

{

Double per;

per=2*Math.PI*radius;

return per;

}

}

 


Inorder to access the data fields and methods in the program we have to create instances of the class which means creating an object.

Syntax for creating object:

classname objectname=new classname();

new is an operator used to create objects.

Creating object for above example:

Circle c1=new Circle();

So, this will be the continuation of above program(writing main() method and creating object):

class MyCircle

{

public static void main(String args[])

{

Circle c1=new Circle();

c1.radius=13;

double a=c1.calArea();

double p=c1.calPerimeter();

System.out.println(“Area of circle= “+a+”Perimeter of circle =” +p);

}

}

So the output will be:

Area of circle= 531.1428571429

Perimeter of circle = 81.7142857143



Do leave your doubts and queries in the comments section .
And please follow and subscribe the blog to continue recieving updates of this and all other courses .

Comments

Popular Posts