Oops 1
Oops 1
=================================
Object-oriented programming (OOP) is a programming method that represents the concept of
"objects" that have data fields (attributes that describe the object) and associated procedures/functions
known as methods.
Syntax:
class classname
{
member variable declarations;
member function definition;
}
Note;
Member of classes has protected access specifier in default.
Object :
~~~~~~
* An object is an instance(Example) of a class.
Object creation = instanciation
* Objects have states and behaviors.
* Class doesn't have memory, but the object has a memory.
Access Specifier :
--------------------
1) private
Variables and functions can be accessed only inside the class.
2) public
Variables and functions can be accessed from anywhere.
3)protected
Variables and functions can be accessed from inside the class and also from the child class.
class bank
{
private int ac_no;
private string name;
p-rivate float balance;
bank b1,b2,b3;
b1=new bank();
b2=new bank();
b3=new bank();
Benefits of OOPS:
1) Data Hiding : Declaring a members of the class with Private keyword established the data hiding.
It is used to hide internal object details to other classes.
Ex:
class bank
{
private int acno;
private int balance;
}
2) Data Encapsulation : Encapsulation is a way of organizing data and methods into a structure by
concealing the the way the object is implemented, i.e. preventing access to data by any means
other than those specified.
Ex:
class book
{
String bname="ponniyin selvan",author="kalki",subj="novel";
int price=1200;
}
book b= new book();
share(b);
3) Data Abstraction :
• Focus on the meaning of the operations (behavior), to avoid over-specification.
• The representation details are confined to only a small set of procedures that create and manipulate
data, and all other access is indirectly via only these procedures.
Ex:
class student
{
String sname; //member variable
int m[]=new int[5];
int tot,avg;
}
4) Polymorphism
5) Inheritance: Creating a class from another class.
Example :
import java.util.*;
class student
String sname,result;
int m1,m2,m3,m4,m5,tot;
float avg;
void read()
sname = s.nextLine();
m1 = s.nextInt();
m2 = s.nextInt();
m3 = s.nextInt();
m4 = s.nextInt();
m5 = s.nextInt();
void calculation()
tot = m1+m2+m3+m4+m5;
avg = tot/5;
if(m1>=35 && m2>=35 && m3>=35 && m4>=35 && m5>=35)
result="Pass";
else
result="Fail";
void display()
student s1,s2,s3;
s2=new student();
s1.read();
s2.read();
s3.read();
s1.calculation();
s2.calculation();
s3.calculation();
s1.display();
s2.display();
s3.display();