SlideShare a Scribd company logo
DEFINING INSTANTIABLE
CLASSES
Chapter 6.6:
Syntax: Defining Class
 General syntax for defining a class is:
modifieropt class ClassIdentifier
{
classMembers:
data declarations
methods definitions
}
 Where
 modifier(s) are used to alter the
behavior of the class
 classMembers consist of data declarations
and/or methods definitions.
Class Definition
 A class can contain data declarations and method
declarations
int size, weight;
char category;
Data declarations
Method declarations
UML Design Specification
UML Class Diagram
Class Name
What data does it need?
What behaviors
will it perform?
Public
methods
Hidden
information
Instance variables -- memory locations
used for storing the information needed.
Methods -- blocks of code used
to perform a specific task.
Class Definition: An
Example
public class Rectangle
{
// data declarations
private double length;
private double width;
//methods definitions
public Rectangle(double l, double w) // Constructor method
{
length = l;
width = w;
} // Rectangle constructor
public double calculateArea()
{
return length * width;
} // calculateArea
} // Rectangle class
Method Definition
public void MethodName() // Method Header
{ // Start of method body
} // End of method body
 The Method Header
modifieropt ResultType MethodName (Formal ParameterList )
public static void main (String argv[ ] )
public void deposit (double amount)
public double calculateArea ( )
Method Header
 A method declaration begins with a method
header
int add (int num1, int num2)
method
name
return
type
Formal parameter
list
The parameter list specifies the type
and name of each parameter
The name of a parameter in the method
declaration is called a formal parameter
Method Body
 The method header is followed by the method
body
int add (int num1, int num2)
{
int sum = num1 + num2;
return sum;
}
The return expression
must be consistent with
the return type
sum is local data
Local data are
created each time
the method is
called, and are
destroyed when it
finishes executing
User-Defined Methods
 Methods can return zero or one value
 Value-returning methods
○ Methods that have a return type
 Void methods
○ Methods that do not have a return type
calculateArea Method.
public double calculateArea()
{
double area;
area = length * width;
return area;
}
Return statement
 Value-returning method uses a return
statement to return its value; it passes a
value outside the method.
 Syntax:return statement
return expr;
 Where expr can be:
 Variable, constant value or expression
User-Defined Methods
 Methods can have zero or >= 1
parameters
 No parameters
○ Nothing inside bracket in method header
 1 or more parameters
○ List the paramater/s inside bracket
Method Parameters
- as input/s to a method
public class Rectangle
{
. . .
public void setWidth(double w)
{
width = w;
}
public void setLength(double l)
{
length = l;
}
. . .
}
Syntax: Formal Parameter
List
 Note: it can be one or more dataType
 Eg.
 setWidth( double w )
 int add (int num1, int num2)
(dataType identifier, dataType identifier....)
Creating Rectangle
Instances
 Create, or instantiate, two instances of the
Rectangle class:
The objects (instances)
store actual values.
Rectangle rectangle1 = new Rectangle(30,10);
Rectangle rectangle2 = new Rectangle(25, 20);
Using Rectangle Instances
 We use a method call to ask each object to tell us
its area:
rectangle1 area 300
rectangle2 area 500Printed output:
System.out.println("rectangle1 area " + rectangle1.calculateArea());
System.out.println("rectangle2 area " + rectangle2.calculateArea());
References to
objects
Method calls
Syntax : Object
Construction
 new ClassName(parameters);
 Example:
 new Rectangle(30, 20);
 new Car("BMW 540ti", 2004);
 Purpose:
 To construct a new object, initialize it with
the construction parameters, and return a
reference to the constructed object.
The RectangleUser Class Definition
public class RectangleUser
{
public static void main(String argv[])
{
Rectangle rectangle1 = new Rectangle(30,10);
Rectangle rectangle2 = new Rectangle(25,20);
System.out.println("rectangle1 area " +
rectangle1.calculateArea());
System.out.println("rectangle2 area " +
rectangle2.calculateArea());
} // main()
} // RectangleUser
An application must
have a main() method
Object
Use
Object
Creation
Class
Definition
Method Call
 Syntax to call a method
methodName(actual parameter list);
Eg.
segi4.setWidth(20.5);
obj.add (25, count);
Formal vs Actual
Parameters
 When a method is called, the actual parameters in
the invocation are copied into the formal
parameters in the method header
int add (int num1, int num2)
{
int sum = num1 + num2;
return sum;
}
total = obj.add(25, count);
Formal vs Actual
Parameters
public class RectangleUser
{
public static void main(String argv[])
{
Rectangle rectangle1 = new Rectangle(30.0,10.0);
System.out.println("rectangle1 area " +
rectangle1.calculateArea());
rectangle1.setWidth(20.0);
System.out.println("rectangle1 area " +
rectangle1.calculateArea());
}
}
Method Overloading
 In Java, within a class, several methods
can have the same name. We called
method overloading
 Two methods are said to have different
formal parameter lists:
 If both methods have a different number of
formal parameters
 If the number of formal parameters is the
same in both methods, the data type of the
formal parameters in the order we list must
differ in at least one position
Method Overloading
 Example:
public void methodABC()
public void methodABC(int x)
public void methodABC(int x, double y)
public void methodABC(double x, int y)
public void methodABC(char x, double y)
public void methodABC(String x,int y)
Java code for overloading
public class Exam
{
public static void main (String [] args)
{
int test1=75, test2=68, total_test1, total_test2;
Exam midsem=new Exam();
total_test1 = midsem.result(test1);
System.out.println("Total test 1 : "+ total_test1);
total_test2 = midsem.result(test1,test2);
System.out.println("Total test 2 : "+ total_test2);
}
int result (int i)
{
return i++;
}
int result (int i, int j)
{
return ++i + j;
}
}
 Output
Total test 1 : 75
Total test 2 : 144
Constructors Revisited
 Properties of constructors:
 Name of constructor same as the name of class
 A constructor,even though it is a method, it has no
type
 Constructors are automatically executed when a
class object is instantiated
 A class can have more than one constructors –
“constructor overloading”
○ which constructor executes depends on the type of
value passed to the constructor when the object is
instantiated
Java code (constructor overloading)
public class Student
{ String name;
int age;
Student(String n, int a)
{ name = n; age = a;
System.out.println ("Name1 :" + name);
System.out.println ("Age1 :" + age);
}
Student(String n)
{
name = n; age = 18;
System.out.println ("Name2 :" + name);
System.out.println ("Age2 :" + age);
}
public static void main (String args[])
{
Student myStudent1=new Student("Adam",22);
Student myStudent2=new Student("Adlin");
}
}
Output:
Name1 :Adam
Age1 :22
Name2 :Adlin
Age2 :18
Object Methods & Class Methods
 Object/Instance methods belong to
objects and can only be applied after the
objects are created.
 They called by the following :
objectName.methodName();
 Class can have its own methods known
as class methods or static methods
Static Methods
 Java supports static methods as well as static variables.
 Static Method:-
 Belongs to class (NOT to objects created from the class)
 Can be called without creating an object/instance of the
class
 To define a static method, put the modifier static in the
method declaration:
 Static methods are called by :
ClassName.methodName();
Java Code (static method)
public class Fish
{
public static void main (String args[])
{
System.out.println ("Flower Horn");
Fish.colour();
}
static void colour ()
{
System.out.println ("Beautiful Colour");
}
}
Output:
Flower Horn
Beautiful Colour

More Related Content

What's hot

Oops concept
Oops conceptOops concept
Lecture 2
Lecture 2Lecture 2
Lecture 2
Avinash Kapse
 
Templates
TemplatesTemplates
11. Objects and Classes
11. Objects and Classes11. Objects and Classes
11. Objects and Classes
Intro C# Book
 
Ifi7184.DT lesson 2
Ifi7184.DT lesson 2Ifi7184.DT lesson 2
Ifi7184.DT lesson 2
Sónia
 
OOP
OOPOOP
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
Mayank Jain
 
Chapter 6.5
Chapter 6.5Chapter 6.5
Chapter 6.5
sotlsoc
 
C++ Returning Objects
C++ Returning ObjectsC++ Returning Objects
C++ Returning Objects
Jay Patel
 
Arrays, Structures And Enums
Arrays, Structures And EnumsArrays, Structures And Enums
Arrays, Structures And Enums
Bhushan Mulmule
 
11. java methods
11. java methods11. java methods
11. java methods
M H Buddhika Ariyaratne
 
Vectors data frames
Vectors data framesVectors data frames
Vectors data frames
FAO
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
Mayank Bhatt
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Harry Potter
 
Intake 38 data access 3
Intake 38 data access 3Intake 38 data access 3
Intake 38 data access 3
Mahmoud Ouf
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
Svetlin Nakov
 

What's hot (16)

Oops concept
Oops conceptOops concept
Oops concept
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Templates
TemplatesTemplates
Templates
 
11. Objects and Classes
11. Objects and Classes11. Objects and Classes
11. Objects and Classes
 
Ifi7184.DT lesson 2
Ifi7184.DT lesson 2Ifi7184.DT lesson 2
Ifi7184.DT lesson 2
 
OOP
OOPOOP
OOP
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Chapter 6.5
Chapter 6.5Chapter 6.5
Chapter 6.5
 
C++ Returning Objects
C++ Returning ObjectsC++ Returning Objects
C++ Returning Objects
 
Arrays, Structures And Enums
Arrays, Structures And EnumsArrays, Structures And Enums
Arrays, Structures And Enums
 
11. java methods
11. java methods11. java methods
11. java methods
 
Vectors data frames
Vectors data framesVectors data frames
Vectors data frames
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Intake 38 data access 3
Intake 38 data access 3Intake 38 data access 3
Intake 38 data access 3
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
 

Similar to Chapter 6.6

Object and class
Object and classObject and class
Object and class
mohit tripathi
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
Abou Bakr Ashraf
 
Unit 1 Part - 3 constructor Overloading Static.ppt
Unit 1 Part - 3  constructor Overloading Static.pptUnit 1 Part - 3  constructor Overloading Static.ppt
Unit 1 Part - 3 constructor Overloading Static.ppt
DeepVala5
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
BG Java EE Course
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
Palak Sanghani
 
static methods
static methodsstatic methods
static methods
Micheal Ogundero
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
MuhammadTalha436
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
Shalabh Chaudhary
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08
Terry Yoast
 
Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 new
sotlsoc
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
trixiacruz
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
Renas Rekany
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
Vince Vo
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
Eduardo Bergavera
 
Java Reflection
Java ReflectionJava Reflection
Java Reflection
elliando dias
 
Getters_And_Setters.pptx
Getters_And_Setters.pptxGetters_And_Setters.pptx
Getters_And_Setters.pptx
Kavindu Sachinthe
 
class object.pptx
class object.pptxclass object.pptx
class object.pptx
Killmekhilati
 
C# p8
C# p8C# p8
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptx
RAJASEKHARV10
 
08 class and object
08   class and object08   class and object
08 class and object
dhrubo kayal
 

Similar to Chapter 6.6 (20)

Object and class
Object and classObject and class
Object and class
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
Unit 1 Part - 3 constructor Overloading Static.ppt
Unit 1 Part - 3  constructor Overloading Static.pptUnit 1 Part - 3  constructor Overloading Static.ppt
Unit 1 Part - 3 constructor Overloading Static.ppt
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
 
static methods
static methodsstatic methods
static methods
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08
 
Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 new
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
 
Java Reflection
Java ReflectionJava Reflection
Java Reflection
 
Getters_And_Setters.pptx
Getters_And_Setters.pptxGetters_And_Setters.pptx
Getters_And_Setters.pptx
 
class object.pptx
class object.pptxclass object.pptx
class object.pptx
 
C# p8
C# p8C# p8
C# p8
 
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptx
 
08 class and object
08   class and object08   class and object
08 class and object
 

More from sotlsoc

Chapter 2.0 new
Chapter 2.0 newChapter 2.0 new
Chapter 2.0 new
sotlsoc
 
Chapter 1.0 new
Chapter 1.0 newChapter 1.0 new
Chapter 1.0 new
sotlsoc
 
Chapter 3.0 new
Chapter 3.0 newChapter 3.0 new
Chapter 3.0 new
sotlsoc
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1
sotlsoc
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
sotlsoc
 
Chapter 11.4
Chapter 11.4Chapter 11.4
Chapter 11.4
sotlsoc
 
Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3
sotlsoc
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
sotlsoc
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2
sotlsoc
 
Chapter 11.0
Chapter 11.0Chapter 11.0
Chapter 11.0
sotlsoc
 
Chapter 10.2
Chapter 10.2Chapter 10.2
Chapter 10.2
sotlsoc
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1
sotlsoc
 
Chapter 8.0
Chapter 8.0Chapter 8.0
Chapter 8.0
sotlsoc
 
Chapter 10.0
Chapter 10.0Chapter 10.0
Chapter 10.0
sotlsoc
 
Chapter 9.3
Chapter 9.3Chapter 9.3
Chapter 9.3
sotlsoc
 
Chapter 9.4
Chapter 9.4Chapter 9.4
Chapter 9.4
sotlsoc
 
Chapter 9.1
Chapter 9.1Chapter 9.1
Chapter 9.1
sotlsoc
 
Chapter 8.1
Chapter 8.1Chapter 8.1
Chapter 8.1
sotlsoc
 
Chapter 9.0
Chapter 9.0Chapter 9.0
Chapter 9.0
sotlsoc
 
Chapter 9.2
Chapter 9.2Chapter 9.2
Chapter 9.2
sotlsoc
 

More from sotlsoc (20)

Chapter 2.0 new
Chapter 2.0 newChapter 2.0 new
Chapter 2.0 new
 
Chapter 1.0 new
Chapter 1.0 newChapter 1.0 new
Chapter 1.0 new
 
Chapter 3.0 new
Chapter 3.0 newChapter 3.0 new
Chapter 3.0 new
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
 
Chapter 11.4
Chapter 11.4Chapter 11.4
Chapter 11.4
 
Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2
 
Chapter 11.0
Chapter 11.0Chapter 11.0
Chapter 11.0
 
Chapter 10.2
Chapter 10.2Chapter 10.2
Chapter 10.2
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1
 
Chapter 8.0
Chapter 8.0Chapter 8.0
Chapter 8.0
 
Chapter 10.0
Chapter 10.0Chapter 10.0
Chapter 10.0
 
Chapter 9.3
Chapter 9.3Chapter 9.3
Chapter 9.3
 
Chapter 9.4
Chapter 9.4Chapter 9.4
Chapter 9.4
 
Chapter 9.1
Chapter 9.1Chapter 9.1
Chapter 9.1
 
Chapter 8.1
Chapter 8.1Chapter 8.1
Chapter 8.1
 
Chapter 9.0
Chapter 9.0Chapter 9.0
Chapter 9.0
 
Chapter 9.2
Chapter 9.2Chapter 9.2
Chapter 9.2
 

Recently uploaded

PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 

Recently uploaded (20)

PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 

Chapter 6.6

  • 2. Syntax: Defining Class  General syntax for defining a class is: modifieropt class ClassIdentifier { classMembers: data declarations methods definitions }  Where  modifier(s) are used to alter the behavior of the class  classMembers consist of data declarations and/or methods definitions.
  • 3. Class Definition  A class can contain data declarations and method declarations int size, weight; char category; Data declarations Method declarations
  • 4. UML Design Specification UML Class Diagram Class Name What data does it need? What behaviors will it perform? Public methods Hidden information Instance variables -- memory locations used for storing the information needed. Methods -- blocks of code used to perform a specific task.
  • 5. Class Definition: An Example public class Rectangle { // data declarations private double length; private double width; //methods definitions public Rectangle(double l, double w) // Constructor method { length = l; width = w; } // Rectangle constructor public double calculateArea() { return length * width; } // calculateArea } // Rectangle class
  • 6. Method Definition public void MethodName() // Method Header { // Start of method body } // End of method body  The Method Header modifieropt ResultType MethodName (Formal ParameterList ) public static void main (String argv[ ] ) public void deposit (double amount) public double calculateArea ( )
  • 7. Method Header  A method declaration begins with a method header int add (int num1, int num2) method name return type Formal parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal parameter
  • 8. Method Body  The method header is followed by the method body int add (int num1, int num2) { int sum = num1 + num2; return sum; } The return expression must be consistent with the return type sum is local data Local data are created each time the method is called, and are destroyed when it finishes executing
  • 9. User-Defined Methods  Methods can return zero or one value  Value-returning methods ○ Methods that have a return type  Void methods ○ Methods that do not have a return type
  • 10. calculateArea Method. public double calculateArea() { double area; area = length * width; return area; }
  • 11. Return statement  Value-returning method uses a return statement to return its value; it passes a value outside the method.  Syntax:return statement return expr;  Where expr can be:  Variable, constant value or expression
  • 12. User-Defined Methods  Methods can have zero or >= 1 parameters  No parameters ○ Nothing inside bracket in method header  1 or more parameters ○ List the paramater/s inside bracket
  • 13. Method Parameters - as input/s to a method public class Rectangle { . . . public void setWidth(double w) { width = w; } public void setLength(double l) { length = l; } . . . }
  • 14. Syntax: Formal Parameter List  Note: it can be one or more dataType  Eg.  setWidth( double w )  int add (int num1, int num2) (dataType identifier, dataType identifier....)
  • 15. Creating Rectangle Instances  Create, or instantiate, two instances of the Rectangle class: The objects (instances) store actual values. Rectangle rectangle1 = new Rectangle(30,10); Rectangle rectangle2 = new Rectangle(25, 20);
  • 16. Using Rectangle Instances  We use a method call to ask each object to tell us its area: rectangle1 area 300 rectangle2 area 500Printed output: System.out.println("rectangle1 area " + rectangle1.calculateArea()); System.out.println("rectangle2 area " + rectangle2.calculateArea()); References to objects Method calls
  • 17. Syntax : Object Construction  new ClassName(parameters);  Example:  new Rectangle(30, 20);  new Car("BMW 540ti", 2004);  Purpose:  To construct a new object, initialize it with the construction parameters, and return a reference to the constructed object.
  • 18. The RectangleUser Class Definition public class RectangleUser { public static void main(String argv[]) { Rectangle rectangle1 = new Rectangle(30,10); Rectangle rectangle2 = new Rectangle(25,20); System.out.println("rectangle1 area " + rectangle1.calculateArea()); System.out.println("rectangle2 area " + rectangle2.calculateArea()); } // main() } // RectangleUser An application must have a main() method Object Use Object Creation Class Definition
  • 19. Method Call  Syntax to call a method methodName(actual parameter list); Eg. segi4.setWidth(20.5); obj.add (25, count);
  • 20. Formal vs Actual Parameters  When a method is called, the actual parameters in the invocation are copied into the formal parameters in the method header int add (int num1, int num2) { int sum = num1 + num2; return sum; } total = obj.add(25, count);
  • 21. Formal vs Actual Parameters public class RectangleUser { public static void main(String argv[]) { Rectangle rectangle1 = new Rectangle(30.0,10.0); System.out.println("rectangle1 area " + rectangle1.calculateArea()); rectangle1.setWidth(20.0); System.out.println("rectangle1 area " + rectangle1.calculateArea()); } }
  • 22. Method Overloading  In Java, within a class, several methods can have the same name. We called method overloading  Two methods are said to have different formal parameter lists:  If both methods have a different number of formal parameters  If the number of formal parameters is the same in both methods, the data type of the formal parameters in the order we list must differ in at least one position
  • 23. Method Overloading  Example: public void methodABC() public void methodABC(int x) public void methodABC(int x, double y) public void methodABC(double x, int y) public void methodABC(char x, double y) public void methodABC(String x,int y)
  • 24. Java code for overloading public class Exam { public static void main (String [] args) { int test1=75, test2=68, total_test1, total_test2; Exam midsem=new Exam(); total_test1 = midsem.result(test1); System.out.println("Total test 1 : "+ total_test1); total_test2 = midsem.result(test1,test2); System.out.println("Total test 2 : "+ total_test2); } int result (int i) { return i++; } int result (int i, int j) { return ++i + j; } }
  • 25.  Output Total test 1 : 75 Total test 2 : 144
  • 26. Constructors Revisited  Properties of constructors:  Name of constructor same as the name of class  A constructor,even though it is a method, it has no type  Constructors are automatically executed when a class object is instantiated  A class can have more than one constructors – “constructor overloading” ○ which constructor executes depends on the type of value passed to the constructor when the object is instantiated
  • 27. Java code (constructor overloading) public class Student { String name; int age; Student(String n, int a) { name = n; age = a; System.out.println ("Name1 :" + name); System.out.println ("Age1 :" + age); } Student(String n) { name = n; age = 18; System.out.println ("Name2 :" + name); System.out.println ("Age2 :" + age); } public static void main (String args[]) { Student myStudent1=new Student("Adam",22); Student myStudent2=new Student("Adlin"); } }
  • 29. Object Methods & Class Methods  Object/Instance methods belong to objects and can only be applied after the objects are created.  They called by the following : objectName.methodName();  Class can have its own methods known as class methods or static methods
  • 30. Static Methods  Java supports static methods as well as static variables.  Static Method:-  Belongs to class (NOT to objects created from the class)  Can be called without creating an object/instance of the class  To define a static method, put the modifier static in the method declaration:  Static methods are called by : ClassName.methodName();
  • 31. Java Code (static method) public class Fish { public static void main (String args[]) { System.out.println ("Flower Horn"); Fish.colour(); } static void colour () { System.out.println ("Beautiful Colour"); } }