SlideShare a Scribd company logo
1 of 23
Chapter Seven Java
Class
The class is at the core of Java. It is the logical
construct upon which the entire Java language is
built because it defines the shape and nature of an
object.
As such, the class forms the basis for object-
oriented programming in Java.
Any concept you wish to implement in a Java
program must be encapsulated within a class.
Continue..
Perhaps the most important thing to understand
about a class is that it defines a new data type. Once
defined, this new type can be used to create objects
of that type.
Thus, a class is a template for an object, and an
object is an instance of a class.
Because an object is an instance of a class, you will
often see the two words object and instance used
interchangeably.
General form of a class
type methodname2(parameter-list) {
// body of method
}
// ...
type methodnameN(parameter-list)
{
// body of method
}
}
class classname {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-
list) {
// body of method
Continue..
When you define a class, you declare its exact
form and nature. You do this by specifying the
data that it contains and the code that operates
on that data.
While very simple classes may contain only code
or only data, most real-world classes contain
both.
Continue..
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.
Continue..
In most classes, the instance variables are acted
upon and accessed by the methods defined for
that class.
Thus, as a general rule, it is the methods that
determine how a class’ data can be used.
Continue..
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.
Thus, the data for one object is separate and
unique from the data for another.
We will come back to this point shortly, but it is an
important concept to learn early.
Declaring Object
Obtaining objects of a class is a two-step process.
First, you must declare a variable of the class type.
This variable does not define an object.
Instead, it is simply a variable that can refer to an
object.
Second, you must acquire an actual, physical copy of
the object and assign it to that variable.
Continue..
You can do this using the new operator. The new
operator dynamically allocates (that is, allocates at
run time) memory for an object and returns a
reference to it.
This reference is, more or less, the address in
memory of the object allocated by new.
This reference is then stored in the variable. Thus, in
Java, all class objects must be dynamically allocated.
Continue..
Assigning Object Reference Variables
Object reference variables act differently than you
might expect when an assignment takes place.
For example, what do you think the following
fragment does?
Box b1 = new Box();
Box b2 = b1;
Continue..
You might think that b2 is being assigned a
reference to a copy of the object referred to by b1.
That is, you might think that b1 and b2 refer to
separate and distinct objects.
However, this would be wrong. Instead, after this
fragment executes, b1 and b2 will both refer to the
same object.
Continue..
The assignment of b1 to b2 did not allocate any
memory or copy any part of the original object.
It simply makes b2 refer to the same object as does
b1.
Thus, any changes made to the object through b2 will
affect the object to which b1 is referring, since They
are the same object.
Continue..
Methods
As mentioned at the beginning of this chapter, classes
usually consist of two things: instance variables and
methods.
The topic of methods is a large one because Java
gives them so much power and flexibility.
Continue..
In fact, much of the next chapter is devoted to
methods.
However, there are some fundamentals that you
need to learn now so that you can begin to add
methods to your classes.
Continue..
The general form of a method is :
Return_type name(parameter-list) {
// body of method
}
Continue..
Here, type specifies the type of data returned by the
method.
This can be any valid type, including class types that
you create.
If the method does not return a value, its return
type must be void.
The name of the method is specified by name.
Continue..
This can be any legal identifier other than those
already used by other items within the current scope.
The parameter-list is a sequence of type and
identifier pairs separated by commas.
Parameters are essentially variables that receive the
value of the arguments passed to the method when it
is called.
Continue..
If the method has no parameters, then the
parameter list will be empty.
Methods that have a return type other than void
return a value to the calling routine using the
following form of the return statement:
return value;
Here, value is the value returned.
Continue..
In the next few examples, you will see how to create
various types of methods, including those that take
parameters and those that return values.
Thank you

More Related Content

What's hot

Mutable and immutable classes
Mutable and  immutable classesMutable and  immutable classes
Mutable and immutable classes
Tech_MX
 

What's hot (20)

object oriented programming using c++
 object oriented programming using c++ object oriented programming using c++
object oriented programming using c++
 
2.oop concept
2.oop concept2.oop concept
2.oop concept
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
Object and class
Object and classObject and class
Object and class
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | Edureka
 
Python - object oriented
Python - object orientedPython - object oriented
Python - object oriented
 
Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1 Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Chapter 8 java
Chapter 8 javaChapter 8 java
Chapter 8 java
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
 
Basic concept of Object Oriented Programming
Basic concept of Object Oriented Programming Basic concept of Object Oriented Programming
Basic concept of Object Oriented Programming
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Class or Object
Class or ObjectClass or Object
Class or Object
 
Mutable and immutable classes
Mutable and  immutable classesMutable and  immutable classes
Mutable and immutable classes
 
Keyword of java
Keyword of javaKeyword of java
Keyword of java
 
Class properties
Class propertiesClass properties
Class properties
 
Objects and Types C#
Objects and Types C#Objects and Types C#
Objects and Types C#
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
 

Similar to Chapter 7 java

1 1 5 Clases
1 1 5 Clases1 1 5 Clases
1 1 5 Clases
UVM
 

Similar to Chapter 7 java (20)

packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Java defining classes
Java defining classes Java defining classes
Java defining classes
 
Delphi qa
Delphi qaDelphi qa
Delphi qa
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorial
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
1 1 5 Clases
1 1 5 Clases1 1 5 Clases
1 1 5 Clases
 
Oops
OopsOops
Oops
 
Oops
OopsOops
Oops
 
Ruby Interview Questions
Ruby Interview QuestionsRuby Interview Questions
Ruby Interview Questions
 
Computer Programming 2
Computer Programming 2 Computer Programming 2
Computer Programming 2
 
Classes,object and methods java
Classes,object and methods javaClasses,object and methods java
Classes,object and methods java
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
 
JavaScript OOPS Implimentation
JavaScript OOPS ImplimentationJavaScript OOPS Implimentation
JavaScript OOPS Implimentation
 
Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHP
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Reflection
ReflectionReflection
Reflection
 
(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
 

More from Ahmad sohail Kakar

More from Ahmad sohail Kakar (20)

Lec 1 network types
Lec 1 network typesLec 1 network types
Lec 1 network types
 
Lec 1 introduction
Lec 1 introductionLec 1 introduction
Lec 1 introduction
 
Active directory restoration
Active directory restorationActive directory restoration
Active directory restoration
 
Active directory backup
Active directory backupActive directory backup
Active directory backup
 
Seii unit7 component-level-design
Seii unit7 component-level-designSeii unit7 component-level-design
Seii unit7 component-level-design
 
Seii unit6 software-testing-techniques
Seii unit6 software-testing-techniquesSeii unit6 software-testing-techniques
Seii unit6 software-testing-techniques
 
Seii unit5 ui_design
Seii unit5 ui_designSeii unit5 ui_design
Seii unit5 ui_design
 
Seii unit4 software_process
Seii unit4 software_processSeii unit4 software_process
Seii unit4 software_process
 
Se ii unit3-architectural-design
Se ii unit3-architectural-designSe ii unit3-architectural-design
Se ii unit3-architectural-design
 
Se ii unit2-software_design_principles
Se ii unit2-software_design_principlesSe ii unit2-software_design_principles
Se ii unit2-software_design_principles
 
Se ii unit1-se_ii_intorduction
Se ii unit1-se_ii_intorductionSe ii unit1-se_ii_intorduction
Se ii unit1-se_ii_intorduction
 
Second chapter-java
Second chapter-javaSecond chapter-java
Second chapter-java
 
Second chapter-java
Second chapter-javaSecond chapter-java
Second chapter-java
 
Chapter 9 java
Chapter 9 javaChapter 9 java
Chapter 9 java
 
Chapter 6 java
Chapter 6 javaChapter 6 java
Chapter 6 java
 
Chapter 5 java
Chapter 5 javaChapter 5 java
Chapter 5 java
 
Chapter 4 java
Chapter 4 javaChapter 4 java
Chapter 4 java
 
Chapter 3 java
Chapter 3 javaChapter 3 java
Chapter 3 java
 
Chapter 2 java
Chapter 2 javaChapter 2 java
Chapter 2 java
 
Chapter 1 java
Chapter 1 java Chapter 1 java
Chapter 1 java
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 

Chapter 7 java

  • 2. Class The class is at the core of Java. It is the logical construct upon which the entire Java language is built because it defines the shape and nature of an object. As such, the class forms the basis for object- oriented programming in Java. Any concept you wish to implement in a Java program must be encapsulated within a class.
  • 3. Continue.. Perhaps the most important thing to understand about a class is that it defines a new data type. Once defined, this new type can be used to create objects of that type. Thus, a class is a template for an object, and an object is an instance of a class. Because an object is an instance of a class, you will often see the two words object and instance used interchangeably.
  • 4. General form of a class type methodname2(parameter-list) { // body of method } // ... type methodnameN(parameter-list) { // body of method } } class classname { type instance-variable1; type instance-variable2; // ... type instance-variableN; type methodname1(parameter- list) { // body of method
  • 5. Continue.. When you define a class, you declare its exact form and nature. You do this by specifying the data that it contains and the code that operates on that data. While very simple classes may contain only code or only data, most real-world classes contain both.
  • 6. Continue.. 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.
  • 7. Continue.. In most classes, the instance variables are acted upon and accessed by the methods defined for that class. Thus, as a general rule, it is the methods that determine how a class’ data can be used.
  • 8. Continue.. 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. Thus, the data for one object is separate and unique from the data for another. We will come back to this point shortly, but it is an important concept to learn early.
  • 9. Declaring Object Obtaining objects of a class is a two-step process. First, you must declare a variable of the class type. This variable does not define an object. Instead, it is simply a variable that can refer to an object. Second, you must acquire an actual, physical copy of the object and assign it to that variable.
  • 10. Continue.. You can do this using the new operator. The new operator dynamically allocates (that is, allocates at run time) memory for an object and returns a reference to it. This reference is, more or less, the address in memory of the object allocated by new. This reference is then stored in the variable. Thus, in Java, all class objects must be dynamically allocated.
  • 12. Assigning Object Reference Variables Object reference variables act differently than you might expect when an assignment takes place. For example, what do you think the following fragment does? Box b1 = new Box(); Box b2 = b1;
  • 13. Continue.. You might think that b2 is being assigned a reference to a copy of the object referred to by b1. That is, you might think that b1 and b2 refer to separate and distinct objects. However, this would be wrong. Instead, after this fragment executes, b1 and b2 will both refer to the same object.
  • 14. Continue.. The assignment of b1 to b2 did not allocate any memory or copy any part of the original object. It simply makes b2 refer to the same object as does b1. Thus, any changes made to the object through b2 will affect the object to which b1 is referring, since They are the same object.
  • 16. Methods As mentioned at the beginning of this chapter, classes usually consist of two things: instance variables and methods. The topic of methods is a large one because Java gives them so much power and flexibility.
  • 17. Continue.. In fact, much of the next chapter is devoted to methods. However, there are some fundamentals that you need to learn now so that you can begin to add methods to your classes.
  • 18. Continue.. The general form of a method is : Return_type name(parameter-list) { // body of method }
  • 19. Continue.. Here, type specifies the type of data returned by the method. This can be any valid type, including class types that you create. If the method does not return a value, its return type must be void. The name of the method is specified by name.
  • 20. Continue.. This can be any legal identifier other than those already used by other items within the current scope. The parameter-list is a sequence of type and identifier pairs separated by commas. Parameters are essentially variables that receive the value of the arguments passed to the method when it is called.
  • 21. Continue.. If the method has no parameters, then the parameter list will be empty. Methods that have a return type other than void return a value to the calling routine using the following form of the return statement: return value; Here, value is the value returned.
  • 22. Continue.. In the next few examples, you will see how to create various types of methods, including those that take parameters and those that return values.