Day21: Object Oriented Programming System(OOPs)

Object Oriented Programming System(OOPs) 

Object Oriented Programming is not a programming Language. Rather it is a methodology approach to design programs which are close to read world and help us visualize real world situation in our software.

The biggest advantage of Object oriented programming-
  1. Data Security
  2. Easy Modeling of Real world situation

What are the two basic building block of OOPs?

The most important component of object oriented programming are:
  1. Classes
  2. Objects

Class:

A class is just like a blueprint Or Template which describes how an object will look.
For Example : 
    Model of building, Develop by civil engineer is like a class while actual building built up by the Labourers is object.
In Simple terms we can say classes is logical while object are physical.

What does a class contain?

A class contain two important element.
(1) Instance Variable -
        They are also called data member in languages like C++ and they hold the actual data stored in an object.
(2) Method - 
They are operation which an object can perform in simple terms they are function associated with an object.

Object

As mention above a class is a logical unit so it never physically exist in memory. Thus in order to use a class we have to instantiate the class. The word instantiation simply means creating object of the class.

Syntax Creating(Defining) a class : 

<access_Modifier> class <class_name>
{
<access modifier> <data_type> <variable> = value;
<access modifier> <data_type> <variable> = value;
<access modifier> <data_type> <variable> = value;
<access_modifier><return_type>method-name(<args>)
{
//Body
}
<access modifier><return-type>method_name(<args>)
{
//Body
}
}

Defining Student Class

First we Create a file name called Student.java and below is the code which I can declared by the above syntax.
package SCA;
//Example of creating a class
public class Student {
public int roll;
public char grade;
public double per;
}

Then after that we've to create driver class which can I access by Student Class followed by above syntax and named them always as UseStudent.java. Actually what happen the reason behind them to naming driver class name in terms of class name( like Student) shows a good programmer and Java recommend this also to create driver class with Use and here you can't coding with your own purpose, you can code with this several project and if you choosing another name that is not recommended then the other sider the programmer not understand what's driver class you'll used.
File name : UseStudent.java
public class UseStudent
{
public static void main(String[] args)
{
Student S;
S = new Student();
S.roll = 10;
S.grade = 'A';
S.per = 90.50;

System.out.println(S.roll + " " + S.grade + " " + S.per);
}
}
Output : 
10 A 90.5

Drawback with previous code:

Although the above code is successfully compiled and executed but it is breaking a very important principle of OOPs called encapsulation.


What is Encapsulation?

Encapsulation is one of the most important principle of object oriented programming which is derived from a word called CAPSULE. As we know a capsule represents mixture of multiple medicines packed in a single unit, similarly every program is a collection of two elements data and function. The word data represent variable (Instance variable in Java) and the word function represents method since both data and function always work together as OOPs says that they must be decrease together also.
        Thus declaring data and method within  a class as a single unit called as ENCAPSULATION.

Benefit :

The most important benefit offered by encapsulation is data security. This is because we can declare data member (instance variable in java) as private and provides public method to access them. Due to this these data member can't be access directly from outside the class and will remain safe and secure.

How to achieve encapsulation in Java?

In java to achieve encapsulation we've to take two steps while designing the entity class.

  1. Every instance variable must be declared as private.
  2. For every operation we must provide a public method in a class.
File name : Student.java
package SCA;

public class Student {
public int roll;
public char grade;
public double per;

public void setStudent()
{
roll = 10;
grade = 'A';
per = 78.5;
}

public void showStudent()
{
System.out.println(roll + " " + grade + " " + per);
}
}


File name : UseStudent.java
package SCA;
//Creating Driver Class
public class UseStudent
{
public static void main(String[] args)
{
Student S;
S = new Student();
S.setStudent();
S.showStudent();
}
}
Output of Above Code : 10  A  78.5

Creating Parameterized method in Entity Class:

File Name : Student.java
package SCA;
public class Student {
private int roll;
private String name;
private double per;

public void setStudent(int r, String str, double p)
{
roll = r;
name = str;
per = p;
}

public void showStudent()
{
System.out.println("Roll = "+roll+ " name = "+name+" percentage = "+per);
}
}

File Name: UseStudent.java
package SCA;

import java.util.Scanner;

//Creating Driver Class
public class UseStudent
{
public static void main(String[] args)
{
Student S;
S = new Student();
Scanner kb = new Scanner(System.in);
int rl;
String nm;
double pr;
System.out.println("Enter roll, name and per ");
rl = kb.nextInt();
kb.nextLine();
nm = kb.nextLine();
pr = kb.nextDouble();

S.setStudent(rl,nm,pr);
S.showStudent();

}
}

Output : 

Enter roll, name and per 
1
Amit
99.6
Roll = 1 name = Amit percentage = 99.6







 

Comments

Popular posts from this blog

Day2 : Compiling Java Program with Examples

Day1: Introduction to Java and its feature

What does System.out.printf( "%-15s%03d\n", s1, x) do?