Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Classes Objects

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

Classes and Objects

Classes and Objects


⚫ Java is an Object-Oriented programming
language
⚫ Object − Objects have states and behaviors. Example:
A dog has states - color, name, breed as well as behaviors –
wagging the tail, barking, eating. An object is an
instance of a class.
⚫ Class − A class can be defined as a template/blueprint
that describes the behavior/state that the object of its
type support.
Classes
⚫ A class is a group of objects which have common
properties. It is a template or blueprint from which
objects are created. It is a logical entity. It can't be
physical. A class in Java can contain:
⚫ fields
⚫ methods
⚫ constructors
⚫ Blocks
class
< class_name> {
field;
method;
}
Objects
⚫ It is a basic unit of Object Oriented Programming
and represents the real life entities. A typical Java
program creates many objects, which as you know,
interact by invoking methods. An object consist
of :
⚫ State : It is represented by attributes of an object. It
also reflect
the properties of an object.
⚫ Behavior : It is represented by methods of an object. It
also reflects the response of an object with other objects.
⚫ Identity : It gives a unique name to an object and enables
one object to interact with other objects.
Objects
Objects
⚫ Objects correspond to things found in the real world.
⚫ For example, a graphics program may have objects
such as “circle”, “square”, “menu”.
⚫ An online shopping system might have objects such
as “shopping cart”, “customer”, and “product”
⚫ When an object of a class is created, the class is said
to be instantiated.
⚫ All the instances share the attributes and the behavior
of the class. But the values of those attributes, i.e. the
state are unique for each object.
⚫ A single class may have any number of instances.
Objects
Object and Class Example: main outside
class
class Student
{ int id; String name;
}
class TestStudent1{
public static void main(String args[]){
Student s1=new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}
Main inside the class:
public static void main(String args[]) {
//declaring the objects of the class City
//creating a class named City City metro1,metro2;
public class City
{ //Instantiating the objects of the class using the
//declaring class variables new keyword
public String name; metro1 = new City();
public long population; metro2 = new City();

//defining the method of the class metro1.name ="Delhi";


public void display() metro1.population = 10000000;
{ System.out.println("Details of metro city 1:");
System.out.println("City name: " metro1.display(); //display() method is being
+name); invoked for the object metro1
System.out.println("Population: "
+population); metro2.name ="Bangalore";
} metro2.population = 5000000;
System.out.println("Details of metro city 2:");
metro2.display(); //display() method is being
invoked for the object metro2
Object and Class Example: Rectangle
class Rectangle{
class TestRectangle1{
int length;
public static void main(String
int width;
args[]){ Rectangle r1=new
void insert(int l, int w){
Rectangle(); Rectangle r2=new
length=l;
Rectangle(); r1.insert(11,5);
width=w;
r2.insert(3,15);
}
r1.calculateArea(
void calculateArea()
);
{
r2.calculateArea(
System.out.println(length*width);
);
}
}
}
}
Anonymous object [If you have to use an object
only once, an anonymous object is a good
approach.]
class Calculation{
void fact(int n)
{ int fact=1;
for(int i=1;i<=n;i++)
{ fact=fact*i;
}
System.out.println("fact
orial is "+fact);
}
public static void
main(String args[]){
new
Calculation().fact(5);//cal
ling method with
anonymous object
Array of objects
Public Class Account
{
Char Name[20];
int Account Type; AC[0].Name ox10 Arunkumar
int Account AC[0].Account Type ox20 1
Number; float
AC[0].AccountNumber ox22 1234567
Balance
AC[0].Balance ox24 10000.00

Input( ); AC[1].Name ox40 arun


Deposit (int x ); AC[1].Account Type ox60 2
Withdraw (int AC[1].AccountNumber ox62 1234568
x ); Enquire ( ); AC[1].Balance ox64 10000.00
};
Account AC[0]; AC[2].Name ox70 kumar
Account AC[1]; AC[2].Account Type ox90 3
AC[2].AccountNumber ox92 1234569
Account AC[2]; AC[2].Balance ox94 10000.00
Public Class
Account public void enquire()
{ int Account {
String
int Name;
Account Type;float Balance;
Number;
System.out.println(balance);
public Account(String name, int act, }
int acn, float bal)
Public static void main(String args[ ])
{
{
this.name=name
Account AC[] =new Account[3]
this.account type=act;
AC[0] = new Account (“abc”,1, 1234,
this.account
100.00) AC[1] =new Account(“def”,2, 123,
number=acn;
100.00)
this.balance=bal;
AC[2] =new Account(“ghi”,2, 12,
}
100.00) For(int i=0;i<3;i++)
Public void deposit( int x)
AC[i].Deposit(500);
{
balance +=x;
For(int i=0;i<3;i++)
}
AC[i]. Withdraw (500);
Public void withdraw(int
x) }
{ }
balance-=x;
Access Specifiers
Java Access Specifiers (also known as Visibility Specifiers ) regulate access to classes,
fields and methods in Java.
These Specifiers determine whether a field or method in a class, can be used or invoked
by another method in another class or sub-class.
Access Specifiers can be used to restrict access. Access Specifiers are an integral part of
object-oriented programming.

⚫ Public - visible to any class in the Java program


⚫ Private - cannot be accessed by anywhere outside the enclosing class.
⚫ Protected -
⚫ Default - accessible only by classes in the same package
Public specifiers
⚫ Public Specifiers achieves the highest level of accessibility.
⚫ Classes, methods, and fields declared as public can be
accessed from any class in the Java program, whether
these classes are in the same package or in another
package.
Privat
ePrivate Specifiers achieves the lowest level of accessibility.

⚫ Private methods and fields can only be accessed within
the same class to which the methods and fields belong.
⚫ Private methods and fields are not visible within
subclasses and are not inherited by subclasses. So, the
private access specifier is opposite to the public access
specifier.
⚫ Using Private Specifier we can achieve encapsulation and
hide data from the outside world.
Protected
⚫ The protected access modifier is specified using the
keyword
protected.
⚫ The methods or data members declared as protected
are accessible within same package or sub
classes in different package.
Defaul
t When no access modifier is specified for a class , method

or data member – It is said to be having the default
access modifier by default.
⚫ The data members, class or methods which are not declared
using any access modifiers i.e. having default access
modifier are accessible only within the same
package.
Access Specifiers table
Static members and methods
Static members belong to the class instead of a specific
instance, this means if you make a member static, you
can access it without object.
Static methods in Java
⚫ Static method in Java is a method which belongs to the class
and
not to the object. A static method can access only static
data.
⚫ It is a method which belongs to the class and not to
the object(instance)
⚫ A static method can access only static data. It can
not access non-
static data (instance variables)
⚫ A static method can call only other static methods
and can not call
a non-static method from it.
⚫ A static method can be accessed directly by the class
name and
doesn’t need any object
//Java Program to demonstrate the use of static variable
class Student{
int rollno;//instance variable
String name;
static String college ="ITS";//static variable
//constructor
Student(int r,String n){
rollno = r;
name = n;
}
//method to display the values
void display (){System.out.println(rollno+" "+name+" "+college);}
}
//Test class to show the values of static variable
public class TestStaticVariable1{
public static void main(String args[]){
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
s1.display();
s2.display();
}
}
Example of a class
Public Class Account
{
Char Name[20];
int Account Type; AC1.Name ox10 Arunkumar
int Account AC1.Account Type ox20 1
Number; float
AC1.AccountNumber ox22 1234567
Balance
AC1.Balance ox24 10000.00

Input( ); AC2.Name ox40 arun


Deposit (int x ); AC2.Account Type ox60 2
Withdraw (int AC2.AccountNumber ox62 1234568
x ); Enquire ( ); AC2.Balance ox64 10000.00
};
Account AC1; AC3.Name ox70 kumar
Account AC2; AC3.Account Type ox90 3
AC3.AccountNumber ox92 1234569
Account AC3; AC3.Balance ox94 10000.00
Public Class
Account public void enquire()
{ int Account {
String
int Name;
Account Type;float Balance;
Number;
System.out.println(balance);
public Account(String name, int act, }
int acn, float bal)
Public static void main(String args[ ])
{
{
this.name=name
Account AC1=new Account(“arun”,1, 1234, 100.00)
this.account type=act;
Account AC2=new Account(“ar”,2, 123, 100.00)
this.account
Account AC3=new Account(“arn”,2, 12,
number=acn;
100.00) AC1.Deposit(500);
this.balance=bal;
AC2.Deposit(500);
}
AC3.Deposit(500);
Public void deposit( int x)
AC1.Withdraw(500);
{
AC2.
balance +=x;
Withdraw(400);
}
AC3.
Public void withdraw(int
x) Withdraw(400);
{ }
balance-=x; }

You might also like