SlideShare a Scribd company logo
CLASSES, OBJECTS AND
METHODS
What is Class?
• A class is a blueprint for the object.
• It determines how an object will behave and what the
object will contain.
• In other words, it is a blueprint or a set of instruction to
build a specific type of object.
Syntax
class <class_name>
{
field;
method;
}
What is an Object?
• An object is a self-contained component which
consists of methods and properties to make a
particular type of data useful.
• Object determines the behavior of the class.
When you send a message to an object, you are
asking the object to invoke or execute one of its
methods.
Syntax
ClassName ReferenceVariable = new ClassName();
What is the Difference Between Object &
Class?
• A class is a blueprint or prototype that
defines the variables and the methods
(functions) common to all objects of a certain
kind.
• An object is a specimen of a class. Software
objects are often used to model real-world
objects you find in everyday life.
• Understand the concept of Java Classes and Objects with an
example.
• Let's take an example of developing a pet management
system, specially meant for dogs.
• You will need various information about the dogs like
different breeds of the dogs, the age, size, etc.
• You need to model real-life beings, i.e., dogs into software
entities.
• You can see the picture of three different breeds of
dogs below.
• Some of the differences you might have listed out
maybe breed, age, size, color, etc.
• If you think for a minute, these differences are also
some common characteristics shared by these dogs.
• These characteristics (breed, age, size, color) can form
a data members for your object.
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
• Next, list out the common behaviors of these
dogs like sleep, sit, eat, etc.
• So these will be the actions of our software
objects.
• Class - Dogs
• Data members - size, age, color, breed, etc.
• Methods- eat, sleep, sit and run.
• Now, for different values of data members (breed size, age, and
color) in Java class, you will get different dog objects.
• You can design any program using this OOPs approach.
Example Code: Class and Object
// Class Declaration
public class Dog {
// Instance Variables
String breed;
String size;
int age;
String color;
// method 1
public String getInfo() {
return ("Breed is: "+breed+" Size is:"+size+" Age is:"+age+" color is: "+color); }
public static void main(String[] args) {
Dog maltese = new Dog();
maltese.breed="Maltese";
maltese.size="Small";
maltese.age=2;
maltese.color="white";
System.out.println(maltese.getInfo()); } }
OUTPUT
Breed is: Maltese Size is:Small Age is:2 color is: white
Object and Class Example: main outside class
In previous program, we are creating main() method inside the class. Now, we create classes and define main() method in
another class. This is a better way than previous one.
// Class Declaration
class Dog {
// Instance Variables
String breed;
String size;
int age;
String color;
// method 1
public String getInfo() {
return ("Breed is: "+breed+" Size is:"+size+" Age is:"+age+" color is: "+color); } }
public class Execute{
public static void main(String[] args) {
Dog maltese = new Dog();
maltese.breed="Maltese";
maltese.size="Small";
maltese.age=2;
maltese.color="white";
System.out.println(maltese.getInfo()); } }
A class in Java can contain:
– fields
– methods
– constructors
– blocks
– nested class and interface
Declaration of Class
• A class is declared by use of the class keyword.
• The class body is enclosed between curly braces { and }.
• The data or variables, defined within a class are called instance variables.
• The code is contained within methods. Collectively, the methods and
variables defined within a class are called members of the class.
Declaration of Instance Variables
• Variables defined within a class are called instance variables
because each instance of the class (that is, each object of the
class) contains its own copy of these variables.
• An instance variable can be declared public or private or
default (no modifier).
• When we do not want our variable’s value to be changed
out-side our class we should declare them private.
• public variables can be accessed and changed from outside of
the class..
Java - Methods
• A Java method is a collection of statements that are grouped together to perform an
operation.
• Methods are bound to a class and they define the behavior of a class.
• When you call the System.out.println() method, for example, the system actually executes
several statements in order to display a message on the console.
Creating Method
Considering the following example to explain the syntax of a method −
Syntax
public int addition(int a, int b)
{
return a+b;
}
Here,
public − modifier
int − return type
addition − name of the method
a, b − formal parameters
int a, int b − list of parameters
Types of Java methods
• Depending on whether a method is defined by the user, or available
in standard library, there are two types of methods:
– Standard Library Methods
– User-defined Methods
Standard Library Methods
• The standard library methods are built-in methods in Java that are
readily available for use.
• These standard libraries come along with the Java Class Library
(JCL) in a Java archive (*.jar) file with JVM and JRE.
For example,
• print() is a method of java.io.PrintSteam. The print("...") prints the
string inside quotation marks.
• sqrt() is a method of Math class. It returns square root of a number.
User-defined Method
• You can also define methods inside a class as per your
wish. Such methods are called user-defined methods.
The complete syntax for defining a Java method is:
modifier static returnType nameOfMethod (Parameter
List)
{
//
method body
}
How to call a Java Method?
• Now you defined a method, you need to use it. For that, you have to call the
method. Here's how:
myFunction();
• This statement calls the myFunction(); method that was declared earlier.
• While Java is executing the program code, it encounters myFunction(); in the code.
• The execution then branches to the myFunction() method, and executes code
inside the body of the method.
• After the codes execution inside the method body is completed, the program
returns to the original state and executes the next statement.
Complete Program of Java Method
class Main
{
public static void main(String[] args)
{
System.out.println("About to encounter a method.");
// method call
myMethod();
System.out.println("Method was executed successfully!");
}
// method definition
private static void myMethod()
{
System.out.println("Printing from inside myMethod()!");
}
}
Output:
About to encounter a method.
Printing from inside myMethod().
Method was executed successfully!
• The method myMethod() in the above program doesn't accept any arguments. Also, the method
doesn't return any value (return type is void).
• Note that, we called the method without creating object of the class. It was possible because
myMethod() is static.
What are the advantages of using methods?
• The main advantage is code reusability. You
can write a method once, and use it multiple
times. You do not have to rewrite the entire
code each time. Think of it as, "write once,
reuse multiple times."
• Methods make code more readable and
easier to debug.
https://www.programiz.com/java-programming/methods

More Related Content

PPTX
class as the basis.pptx
PPTX
Object Oriented Programming - Copy.pptxb
PDF
JAVA Class Presentation.pdf Vsjsjsnheheh
PPTX
Java As an OOP Language,Exception Handling & Applets
PPTX
cs213Lecture_1 java programming oopsss.pptx
PDF
Java unit 7
PPTX
Lecture 5.pptx
PDF
Classes and Object Concept Object Oriented Programming in Java
class as the basis.pptx
Object Oriented Programming - Copy.pptxb
JAVA Class Presentation.pdf Vsjsjsnheheh
Java As an OOP Language,Exception Handling & Applets
cs213Lecture_1 java programming oopsss.pptx
Java unit 7
Lecture 5.pptx
Classes and Object Concept Object Oriented Programming in Java

Similar to 123 JAVA CLASSES, OBJECTS AND METHODS.ppt (20)

PPTX
PPTX
Android Training (Java Review)
PPTX
classes-objects in oops java-201023154255.pptx
PPTX
Java chapter 5
PPTX
introduction_OOP for the java courses [Autosaved].pptx
PPT
Md02 - Getting Started part-2
PDF
7. VARIABLEs presentation in java programming. Pdf
PPT
Java lec class, objects and constructors
PPTX
OCP Java (OCPJP) 8 Exam Quick Reference Card
PDF
JAVA-PPT'S.pdf
PPTX
JAVA-PPT'S-complete-chrome.pptx
PPTX
JAVA-PPT'S.pptx
PPTX
Pi j3.1 inheritance
PPTX
Object oriented programming CLASSES-AND-OBJECTS.pptx
PPTX
Object Oriented Programming Tutorial.pptx
PPTX
Object Oriented Programming Class and Objects
PPTX
UNIT - IIInew.pptx
PPTX
Object Oriented Programming (OOP) Introduction
PPTX
Unit3 part1-class
Android Training (Java Review)
classes-objects in oops java-201023154255.pptx
Java chapter 5
introduction_OOP for the java courses [Autosaved].pptx
Md02 - Getting Started part-2
7. VARIABLEs presentation in java programming. Pdf
Java lec class, objects and constructors
OCP Java (OCPJP) 8 Exam Quick Reference Card
JAVA-PPT'S.pdf
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S.pptx
Pi j3.1 inheritance
Object oriented programming CLASSES-AND-OBJECTS.pptx
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Class and Objects
UNIT - IIInew.pptx
Object Oriented Programming (OOP) Introduction
Unit3 part1-class
Ad

More from mcjaya2024 (20)

PPT
cyber forensics Email Investigations.ppt
PPT
Cell Phone and Mobile Devices Forensics.ppt
PPT
Computer Forensics Analysis and Validation.ppt
PPT
cyber forensics Footprinting and Scanning.ppt
PPT
cyber forensics-enum,sniffing,malware threat.ppt
PPT
Classless Interdomain Data Routing CIDR.ppt
PPT
Computer Network in Network software.ppt
PPT
web program-Extended MARKUP Language XML.ppt
PPTX
Web programming-Introduction to JSP.pptx
PPT
web program -Life cycle of a servlet.ppt
PPT
web programmimg- concpt in JAVABEANS.ppt
PPT
web program-Inheritance,pack&except in Java.ppt
PPT
web programming-Multithreading concept in Java.ppt
PPT
Processing Crime and Incident Scenes.ppt
PPT
Working with Windows and DOS Systems (1).ppt
PDF
enterprise resource plnning ERP vendors.pdf
PPT
ERP and elctronic commerce online12.ppt
PPT
Enterprise resourse planning ERPlife cycle.ppt
PPT
Project Management Issues in ERP IS 6006.ppt
PDF
mySAP_Supply_Chain_Management_Solution_Map.pdf
cyber forensics Email Investigations.ppt
Cell Phone and Mobile Devices Forensics.ppt
Computer Forensics Analysis and Validation.ppt
cyber forensics Footprinting and Scanning.ppt
cyber forensics-enum,sniffing,malware threat.ppt
Classless Interdomain Data Routing CIDR.ppt
Computer Network in Network software.ppt
web program-Extended MARKUP Language XML.ppt
Web programming-Introduction to JSP.pptx
web program -Life cycle of a servlet.ppt
web programmimg- concpt in JAVABEANS.ppt
web program-Inheritance,pack&except in Java.ppt
web programming-Multithreading concept in Java.ppt
Processing Crime and Incident Scenes.ppt
Working with Windows and DOS Systems (1).ppt
enterprise resource plnning ERP vendors.pdf
ERP and elctronic commerce online12.ppt
Enterprise resourse planning ERPlife cycle.ppt
Project Management Issues in ERP IS 6006.ppt
mySAP_Supply_Chain_Management_Solution_Map.pdf
Ad

Recently uploaded (20)

PPTX
Construction Project Organization Group 2.pptx
PDF
Digital Logic Computer Design lecture notes
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPT
Project quality management in manufacturing
PDF
PPT on Performance Review to get promotions
PPTX
Foundation to blockchain - A guide to Blockchain Tech
DOCX
573137875-Attendance-Management-System-original
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Sustainable Sites - Green Building Construction
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
web development for engineering and engineering
PPTX
Geodesy 1.pptx...............................................
PDF
composite construction of structures.pdf
PPTX
UNIT 4 Total Quality Management .pptx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Construction Project Organization Group 2.pptx
Digital Logic Computer Design lecture notes
Automation-in-Manufacturing-Chapter-Introduction.pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Project quality management in manufacturing
PPT on Performance Review to get promotions
Foundation to blockchain - A guide to Blockchain Tech
573137875-Attendance-Management-System-original
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
R24 SURVEYING LAB MANUAL for civil enggi
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
CYBER-CRIMES AND SECURITY A guide to understanding
Sustainable Sites - Green Building Construction
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
web development for engineering and engineering
Geodesy 1.pptx...............................................
composite construction of structures.pdf
UNIT 4 Total Quality Management .pptx
Mechanical Engineering MATERIALS Selection
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx

123 JAVA CLASSES, OBJECTS AND METHODS.ppt

  • 2. What is Class? • A class is a blueprint for the object. • It determines how an object will behave and what the object will contain. • In other words, it is a blueprint or a set of instruction to build a specific type of object. Syntax class <class_name> { field; method; }
  • 3. What is an Object? • An object is a self-contained component which consists of methods and properties to make a particular type of data useful. • Object determines the behavior of the class. When you send a message to an object, you are asking the object to invoke or execute one of its methods. Syntax ClassName ReferenceVariable = new ClassName();
  • 4. What is the Difference Between Object & Class? • A class is a blueprint or prototype that defines the variables and the methods (functions) common to all objects of a certain kind. • An object is a specimen of a class. Software objects are often used to model real-world objects you find in everyday life.
  • 5. • Understand the concept of Java Classes and Objects with an example. • Let's take an example of developing a pet management system, specially meant for dogs. • You will need various information about the dogs like different breeds of the dogs, the age, size, etc. • You need to model real-life beings, i.e., dogs into software entities.
  • 6. • You can see the picture of three different breeds of dogs below. • Some of the differences you might have listed out maybe breed, age, size, color, etc. • If you think for a minute, these differences are also some common characteristics shared by these dogs. • These characteristics (breed, age, size, color) can form a data members for your object.
  • 8. • Next, list out the common behaviors of these dogs like sleep, sit, eat, etc. • So these will be the actions of our software objects.
  • 9. • Class - Dogs • Data members - size, age, color, breed, etc. • Methods- eat, sleep, sit and run.
  • 10. • Now, for different values of data members (breed size, age, and color) in Java class, you will get different dog objects. • You can design any program using this OOPs approach.
  • 11. Example Code: Class and Object // Class Declaration public class Dog { // Instance Variables String breed; String size; int age; String color; // method 1 public String getInfo() { return ("Breed is: "+breed+" Size is:"+size+" Age is:"+age+" color is: "+color); } public static void main(String[] args) { Dog maltese = new Dog(); maltese.breed="Maltese"; maltese.size="Small"; maltese.age=2; maltese.color="white"; System.out.println(maltese.getInfo()); } } OUTPUT Breed is: Maltese Size is:Small Age is:2 color is: white
  • 12. Object and Class Example: main outside class In previous program, we are creating main() method inside the class. Now, we create classes and define main() method in another class. This is a better way than previous one. // Class Declaration class Dog { // Instance Variables String breed; String size; int age; String color; // method 1 public String getInfo() { return ("Breed is: "+breed+" Size is:"+size+" Age is:"+age+" color is: "+color); } } public class Execute{ public static void main(String[] args) { Dog maltese = new Dog(); maltese.breed="Maltese"; maltese.size="Small"; maltese.age=2; maltese.color="white"; System.out.println(maltese.getInfo()); } }
  • 13. A class in Java can contain: – fields – methods – constructors – blocks – nested class and interface
  • 14. Declaration of Class • A class is declared by use of the class keyword. • The class body is enclosed between curly braces { and }. • The data or variables, defined within a class are called instance variables. • The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class.
  • 15. Declaration of Instance Variables • Variables defined within a class are called instance variables because each instance of the class (that is, each object of the class) contains its own copy of these variables. • An instance variable can be declared public or private or default (no modifier). • When we do not want our variable’s value to be changed out-side our class we should declare them private. • public variables can be accessed and changed from outside of the class..
  • 16. Java - Methods • A Java method is a collection of statements that are grouped together to perform an operation. • Methods are bound to a class and they define the behavior of a class. • When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Creating Method Considering the following example to explain the syntax of a method − Syntax public int addition(int a, int b) { return a+b; } Here, public − modifier int − return type addition − name of the method a, b − formal parameters int a, int b − list of parameters
  • 17. Types of Java methods • Depending on whether a method is defined by the user, or available in standard library, there are two types of methods: – Standard Library Methods – User-defined Methods Standard Library Methods • The standard library methods are built-in methods in Java that are readily available for use. • These standard libraries come along with the Java Class Library (JCL) in a Java archive (*.jar) file with JVM and JRE. For example, • print() is a method of java.io.PrintSteam. The print("...") prints the string inside quotation marks. • sqrt() is a method of Math class. It returns square root of a number.
  • 18. User-defined Method • You can also define methods inside a class as per your wish. Such methods are called user-defined methods. The complete syntax for defining a Java method is: modifier static returnType nameOfMethod (Parameter List) { // method body }
  • 19. How to call a Java Method? • Now you defined a method, you need to use it. For that, you have to call the method. Here's how: myFunction(); • This statement calls the myFunction(); method that was declared earlier. • While Java is executing the program code, it encounters myFunction(); in the code. • The execution then branches to the myFunction() method, and executes code inside the body of the method. • After the codes execution inside the method body is completed, the program returns to the original state and executes the next statement.
  • 20. Complete Program of Java Method class Main { public static void main(String[] args) { System.out.println("About to encounter a method."); // method call myMethod(); System.out.println("Method was executed successfully!"); } // method definition private static void myMethod() { System.out.println("Printing from inside myMethod()!"); } } Output: About to encounter a method. Printing from inside myMethod(). Method was executed successfully! • The method myMethod() in the above program doesn't accept any arguments. Also, the method doesn't return any value (return type is void). • Note that, we called the method without creating object of the class. It was possible because myMethod() is static.
  • 21. What are the advantages of using methods? • The main advantage is code reusability. You can write a method once, and use it multiple times. You do not have to rewrite the entire code each time. Think of it as, "write once, reuse multiple times." • Methods make code more readable and easier to debug. https://www.programiz.com/java-programming/methods