SlideShare a Scribd company logo
1 of 20
RAJALAKSHMI ENGINEERING COLLEGE
OPEN ELECTIVE COURSE-OOPS WITH JAVA
CONTENTS
– Classes,Objects and Methods
Classes & Objects
Object
An entity that has state and behavior is known as an object
e.g., chair, bike, marker, pen, table, car,
etc.
It can be physical or logical (tangible and
intangible).
The example of an intangible object is the
banking system.
Cont…
An object has three characteristics:
State: represents the data (value) of an object.
Behavior: represents the behavior (functionality) of
an object such as deposit, withdraw, etc.
Identity: An object identity is typically implemented
via a unique ID. The value of the ID is not visible to
the external user. However, it is used internally by
the JVM to identify each object uniquely.
Cont…
For Example, Pen is an object.
Its name is Reynolds; color is white, known as its state.
It is used to write, so writing is its behavior.
An object is an instance of a class. A class is a template or blueprint from which
objects are created.
So, an object is the instance(result) of a class.
Object Definitions:
An object is a real-world entity.
An object is a runtime entity.
The object is an entity which has state and behavior.
The object is an instance of a class.
Class
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
Nested class and interface
A class contains variable declarations and methoddefinitions
Methods
definitio
ns
Variable
declaratio
ns
Method definitions
Variable declarations
(variable describes the
attributes)
Variable may be: instance variables or static variablesor final
variables
(methods handle the behavior)
Methods may be: instance methods or static methods
Defining a Class in java
Define an Employee class with instance variables and instance methods
class Employee
Instance methods
• setId(…)
• setName(…)
• setSalary(…)
•
getEmployeeDetails()
class Employee{
Instance variables
• id
• name
•salary
int id;
String name;
int salary;
void setId(int i) {
id = i;
}
void setName(String n) {
name = n;
}
void setSalary(int s) {
salary = s;
}
void getEmployeeDetails( ) {
System.out.println (name + “ salary
is “ + salary);
}
}
public class Account
{
double balance;
public void deposit( double amount)
{ balance += amount;}
public double withdraw( double amount ){
int minimum_balance=5000;
if (balance >= (amount+minimum_balance))
{
balance -= amount;
return amount;
Instance
Variable
Parameter
or argument
local
Variable
else {
System.out.println(“Insufficient Balance”);
return 0.0;
}
}
public double getbalance()
{
return balance;
}
}
Basic information about a class (Contd.).
 The previous slide contains definition of a class calledAccounts.
 A class contains members which can either be variables(fields) ormethods(behaviors).
 A variable declared within a class(outside any method) is known as an instancevariable.
 A variable declared within a method is known as localvariable.
 Variables with method declarations are known as parameters or arguments.
 A class variable can also be declared as static where as a local variable cannot bestatic.
Member variables
Objects and References
 Once a class is defined, you can declare a variable (object reference) of typeclass
Student stud1;
Employee emp1;
 The new operator is used to create an object of that reference type
Employee emp = new Employee();
 Object references are used to store objects.
 Reference can be created for any type of classes (like concrete classes, abstract classes)
and interfaces.
object
Object reference
Objects and References (Contd.).
 The new operator,
Dynamically allocates memory for an object
Creates the object on the heap
Returns a reference to it
The reference is then stored in the variable
Object and Class Example: main within the class
class Student
{
int id;//field or data member or instance variable
String name;
public static void main(String args[])
{
Student s1=new Student(); //creating an object of Student
s1.id=1001;
s1.name="John";
System.out.println(s1.id); //accessing member through reference variable
System.out.println(s1.name);
}
}
OUTPUT
1001
John
Object and Class Example: main outside the class
class Student
{
int id;
String name;
}
//Creating another class SampleStudent1 which contains the main method
class SampleStudent1
{
public static void main(String args[])
{
Student s1=new Student();
s1.id=1001;s1.name="John ";
System.out.println(s1.id);
System.out.println(s1.name);
} }
OUTPUT
1001
John
Object and Class Example: Initialization through method
class Student
{
int rollno;
String name;
void insertRecord(int r, String n)
{
rollno=r;
name=n;
}
void displayInformation()
{
System.out.println(rollno+" "+name);
}
}
class SampleStudent
{
public static void main(String args[])
{
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(1001,"Arun");
s2.insertRecord(1002,"Abi");
s1.displayInformation();
s2.displayInformation();
}
}
OUTPUT
1001 Arun
1002 Abi
Example
import java.util.*;
class Rectangle
{
int length;
int width;
void insert(int l, int w)
{
length=l; width=w;
}
int calculateArea()
{
return(length*width);
}
}
public class HelloWorld
{
public static void main(String []args)
{
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
Scanner S=new Scanner(System.in);
int l=S.nextInt();
int b=S.nextInt();
int l1=S.nextInt();
int b1=S.nextInt();
r1.insert(l,b);
r2.insert(l1,b1);
System.out.println("Area of the rectangle1="+r1.calculateArea()+" Sq.units");
System.out.println("Area of the rectangle2="+r2.calculateArea()+" Sq.units");
}
}
OUTPUT
2
5
12
5
Area of the rectangle1=10 Sq.units
Area of the rectangle2=60 Sq.units
public class EmployeeDemo
{
public static void main(String[] args)
{
Employee emp1 = new Employee();
emp1.setId(101);
emp1.setName("John");
emp1.setSalary(12000);
emp1.getEmployeeDetail();
}
}
class Employee
{
int id;
String name;
int salary;
void setId(int no)
{
id = no;
}
void setName(String n)
{ name = n;}
void setSalary(int s)
{ salary = s;}
void getEmployeeDetails()
{
System.out.println(name + " salary is "+ salary);
}
}
Output:
John salary is 12000
Example
Rajalakshmi Engineering College OOPS with Java course overview

More Related Content

Similar to Rajalakshmi Engineering College OOPS with Java course overview

Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorialGhulam Abbas Khan
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
Lecture01 object oriented-programming
Lecture01 object oriented-programmingLecture01 object oriented-programming
Lecture01 object oriented-programmingHariz Mustafa
 
Java is an Object-Oriented Language
Java is an Object-Oriented LanguageJava is an Object-Oriented Language
Java is an Object-Oriented Languageale8819
 
Chapter2 array of objects
Chapter2 array of objectsChapter2 array of objects
Chapter2 array of objectsMahmoud Alfarra
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Getachew Ganfur
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming CourseDennis Chang
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptmanomkpsg
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1Vineeta Garg
 
Unified modeling language
Unified modeling languageUnified modeling language
Unified modeling languageamity2j
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0BG Java EE Course
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxchetanpatilcp783
 
Unified modeling language
Unified modeling languageUnified modeling language
Unified modeling languageamity2j
 

Similar to Rajalakshmi Engineering College OOPS with Java course overview (20)

Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorial
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
Lecture01 object oriented-programming
Lecture01 object oriented-programmingLecture01 object oriented-programming
Lecture01 object oriented-programming
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Java is an Object-Oriented Language
Java is an Object-Oriented LanguageJava is an Object-Oriented Language
Java is an Object-Oriented Language
 
java
javajava
java
 
Chapter2 array of objects
Chapter2 array of objectsChapter2 array of objects
Chapter2 array of objects
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Class and object C++.pptx
Class and object C++.pptxClass and object C++.pptx
Class and object C++.pptx
 
Oop
OopOop
Oop
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
 
Unified modeling language
Unified modeling languageUnified modeling language
Unified modeling language
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptx
 
Unified modeling language
Unified modeling languageUnified modeling language
Unified modeling language
 

Recently uploaded

Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 

Recently uploaded (20)

Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 

Rajalakshmi Engineering College OOPS with Java course overview

  • 1. RAJALAKSHMI ENGINEERING COLLEGE OPEN ELECTIVE COURSE-OOPS WITH JAVA
  • 4. Object An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen, table, car, etc. It can be physical or logical (tangible and intangible). The example of an intangible object is the banking system.
  • 5. Cont… An object has three characteristics: State: represents the data (value) of an object. Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc. Identity: An object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. However, it is used internally by the JVM to identify each object uniquely.
  • 6. Cont… For Example, Pen is an object. Its name is Reynolds; color is white, known as its state. It is used to write, so writing is its behavior. An object is an instance of a class. A class is a template or blueprint from which objects are created. So, an object is the instance(result) of a class. Object Definitions: An object is a real-world entity. An object is a runtime entity. The object is an entity which has state and behavior. The object is an instance of a class.
  • 7. Class 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 Nested class and interface
  • 8. A class contains variable declarations and methoddefinitions Methods definitio ns Variable declaratio ns Method definitions Variable declarations (variable describes the attributes) Variable may be: instance variables or static variablesor final variables (methods handle the behavior) Methods may be: instance methods or static methods
  • 9. Defining a Class in java Define an Employee class with instance variables and instance methods class Employee Instance methods • setId(…) • setName(…) • setSalary(…) • getEmployeeDetails() class Employee{ Instance variables • id • name •salary int id; String name; int salary; void setId(int i) { id = i; } void setName(String n) { name = n; } void setSalary(int s) { salary = s; } void getEmployeeDetails( ) { System.out.println (name + “ salary is “ + salary); } }
  • 10. public class Account { double balance; public void deposit( double amount) { balance += amount;} public double withdraw( double amount ){ int minimum_balance=5000; if (balance >= (amount+minimum_balance)) { balance -= amount; return amount; Instance Variable Parameter or argument local Variable
  • 11. else { System.out.println(“Insufficient Balance”); return 0.0; } } public double getbalance() { return balance; } } Basic information about a class (Contd.).
  • 12.  The previous slide contains definition of a class calledAccounts.  A class contains members which can either be variables(fields) ormethods(behaviors).  A variable declared within a class(outside any method) is known as an instancevariable.  A variable declared within a method is known as localvariable.  Variables with method declarations are known as parameters or arguments.  A class variable can also be declared as static where as a local variable cannot bestatic. Member variables
  • 13. Objects and References  Once a class is defined, you can declare a variable (object reference) of typeclass Student stud1; Employee emp1;  The new operator is used to create an object of that reference type Employee emp = new Employee();  Object references are used to store objects.  Reference can be created for any type of classes (like concrete classes, abstract classes) and interfaces. object Object reference
  • 14. Objects and References (Contd.).  The new operator, Dynamically allocates memory for an object Creates the object on the heap Returns a reference to it The reference is then stored in the variable
  • 15. Object and Class Example: main within the class class Student { int id;//field or data member or instance variable String name; public static void main(String args[]) { Student s1=new Student(); //creating an object of Student s1.id=1001; s1.name="John"; System.out.println(s1.id); //accessing member through reference variable System.out.println(s1.name); } } OUTPUT 1001 John
  • 16. Object and Class Example: main outside the class class Student { int id; String name; } //Creating another class SampleStudent1 which contains the main method class SampleStudent1 { public static void main(String args[]) { Student s1=new Student(); s1.id=1001;s1.name="John "; System.out.println(s1.id); System.out.println(s1.name); } } OUTPUT 1001 John
  • 17. Object and Class Example: Initialization through method class Student { int rollno; String name; void insertRecord(int r, String n) { rollno=r; name=n; } void displayInformation() { System.out.println(rollno+" "+name); } } class SampleStudent { public static void main(String args[]) { Student s1=new Student(); Student s2=new Student(); s1.insertRecord(1001,"Arun"); s2.insertRecord(1002,"Abi"); s1.displayInformation(); s2.displayInformation(); } } OUTPUT 1001 Arun 1002 Abi
  • 18. Example import java.util.*; class Rectangle { int length; int width; void insert(int l, int w) { length=l; width=w; } int calculateArea() { return(length*width); } } public class HelloWorld { public static void main(String []args) { Rectangle r1=new Rectangle(); Rectangle r2=new Rectangle(); Scanner S=new Scanner(System.in); int l=S.nextInt(); int b=S.nextInt(); int l1=S.nextInt(); int b1=S.nextInt(); r1.insert(l,b); r2.insert(l1,b1); System.out.println("Area of the rectangle1="+r1.calculateArea()+" Sq.units"); System.out.println("Area of the rectangle2="+r2.calculateArea()+" Sq.units"); } } OUTPUT 2 5 12 5 Area of the rectangle1=10 Sq.units Area of the rectangle2=60 Sq.units
  • 19. public class EmployeeDemo { public static void main(String[] args) { Employee emp1 = new Employee(); emp1.setId(101); emp1.setName("John"); emp1.setSalary(12000); emp1.getEmployeeDetail(); } } class Employee { int id; String name; int salary; void setId(int no) { id = no; } void setName(String n) { name = n;} void setSalary(int s) { salary = s;} void getEmployeeDetails() { System.out.println(name + " salary is "+ salary); } } Output: John salary is 12000 Example