Objects and Classes in JAVA
OO Programming Concepts
• Object-oriented programming (OOP) involves
programming using objects.
• An object represents an entity in the real
world.
• For example, a student, a desk, a circle, a
button can all be viewed as objects.
• Objects will have state and behavior
• The state of an object (also known as its
properties or attributes) is represented by
data fields with their current values.
• EG: A rectangle object has the data fields
width and height, which are the properties
that characterize a rectangle
• The behavior of an object (also known as its
actions) is defined by methods.
• To invoke a method on an object is to ask the
object to perform an action.
• For example, you may define methods named
getArea() and getPerimeter() for circle
objects.
An object is an instance of a class.
You can create many instances of a class.
Creating an instance is referred to as
instantiation.
Classes
• Classes are constructs that define objects of
the same type.
• A class defines a kind of objects:
– specifies the kinds of attributes (data) an
object of the class can have.
– provides methods specifying the actions an
object of the class can take.
• An object is an instance of the class.
• Person is a class
– Alice and Bob are objects of the Person class.
What does a class have?
• Members of a class:
– Attributes (instance variables, data)
• For each instance of the class (object),
values of attributes can vary, hence
instance variables
– Methods
• Person class
– Attributes: name, address, phone number
– Methods: change address, change phone
number
Example class
Class student {
int id;
String name;
Public static void main(String args[])
{
student s1=new student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}
Class student {
int id;
String name;
}
Class test1
{
public static void main(String args[])
{
student s1=new student();
S1.id=100;
S1.name=“abc”;
System.out.println(“s1.id+ “ “+s1.name+” “);
}
}
Class student {
Int rollno;
String name;
Void read(int r,String n)
{ rollno=r;
name=n;
}
Void display()
{
System.out.println(rollno+” “+name);
}
}
Class student1 {
public static void main(String args[])
{
student s1=new student();
Student s2=new student();
S1.read(100,”Karan”);
S2.read(200,”Aryan”);
S1.display();
S2.display()
}

Objects and Classes in JAVA introduction

  • 1.
  • 2.
    OO Programming Concepts •Object-oriented programming (OOP) involves programming using objects. • An object represents an entity in the real world. • For example, a student, a desk, a circle, a button can all be viewed as objects.
  • 3.
    • Objects willhave state and behavior • The state of an object (also known as its properties or attributes) is represented by data fields with their current values. • EG: A rectangle object has the data fields width and height, which are the properties that characterize a rectangle
  • 4.
    • The behaviorof an object (also known as its actions) is defined by methods. • To invoke a method on an object is to ask the object to perform an action. • For example, you may define methods named getArea() and getPerimeter() for circle objects.
  • 5.
    An object isan instance of a class. You can create many instances of a class. Creating an instance is referred to as instantiation.
  • 6.
    Classes • Classes areconstructs that define objects of the same type.
  • 7.
    • A classdefines a kind of objects: – specifies the kinds of attributes (data) an object of the class can have. – provides methods specifying the actions an object of the class can take. • An object is an instance of the class. • Person is a class – Alice and Bob are objects of the Person class.
  • 8.
    What does aclass have? • Members of a class: – Attributes (instance variables, data) • For each instance of the class (object), values of attributes can vary, hence instance variables – Methods • Person class – Attributes: name, address, phone number – Methods: change address, change phone number
  • 9.
    Example class Class student{ int id; String name; Public static void main(String args[]) { student s1=new student(); System.out.println(s1.id); System.out.println(s1.name); } }
  • 10.
    Class student { intid; String name; } Class test1 { public static void main(String args[]) { student s1=new student(); S1.id=100; S1.name=“abc”; System.out.println(“s1.id+ “ “+s1.name+” “); } }
  • 11.
    Class student { Introllno; String name; Void read(int r,String n) { rollno=r; name=n; } Void display() { System.out.println(rollno+” “+name); } } Class student1 { public static void main(String args[]) { student s1=new student(); Student s2=new student(); S1.read(100,”Karan”); S2.read(200,”Aryan”); S1.display(); S2.display() }