SlideShare a Scribd company logo
1 of 18
Class and Object DhrubojyotiKayal
Class Object Object creation Fields Default values for data members Methods Passing data to methods Agenda
Introduces a new type into the system Defines a template for an object Ex – Circle, Rectangle class ATypeName {  	/* Class body goes here */  	} ATypeName a = new ATypeName();  Class Rectangle { } Rectangle rectangle = new Rectangle(); Class
Software representation of a real world entity Car, Train, Soccer Created from class templates using new operator Object
Objects are manipulated using references Rectangle rectangle = null; Objects must always be created Rectangle rectangle = new Rectangle(); rectangle.draw(); Object creation
This is the data member of the class It can be a primitive or any other object reference class DataOnly {  inti;  	double d;  boolean b;  	Rectangle rectangle; }  DataOnly data = new DataOnly();  data.i = 47;  data.d = 1.1;  data.b = false;  data.rectangle = new Rectangle(); Access – reference.field == discouraged!!! Fields
When a primitive data type is a member of a class, it is guaranteed to get a default value if you do not initialize it Default values for data members
The default values are only what Java guarantees when the variable is used as a member of a class. This ensures that member variables of primitive types will always be initialized (something C++ doesn’t do), reducing a source of bugs  This guarantee doesn’t apply to local variables—those that are not fields of a class  public static void checkthis() { int x; 	x = x + 1;//will not compile } Default values for data members
Function, procedure – a group of statements which manipulate data and optionally return some value. Methods in Java determine the messages an object can receive  the name, the arguments, the return type, and the body  Method
ReturnTypemethodName( /* Argument list */ ) {  	/* Method body */  }  The return type describes the value that comes back from the method after you call it.  The argument list gives the types and names for the information that you want to pass into the method.  The method name and argument list (which is called the signature of the method) uniquely identify that method  objectName.methodName(arg1, arg2, arg3);  int x = a.f();  Method
public class MethodPlayer { 	public boolean play(intvol) { System.out.println(“Play volume -” + vol); 	} 	public static void main(String a[]) { MethodPlayer player = new MethodPlayer(); player.play(5); 	} } Method in Action
Write a Java program that has one member field integer and one method that tests if a given input integer is odd or even. If the given input is even change the value of the member field with this one Now run a for loop to test the first 100 integers with this method. Add another method to get the value of the member field Exercise
A method uses parameters, A called passes arguments You can pass n number of inputs to a method of varying types Java is always pass by value or pass by copy Passing data to methods
public booleanisEven(int value) { 	if(value % 2 ==0) { 		return true; 		//increment and print 		value = value + 1; System.out.println(“Next odd - ” + value); 	} 	else{ 		return false; 	} } inti = 55; isEven(i); System.out.println(i); Passing primitives
Passing primitives 55 int x isEven(i); Copy the value 55 int value 56 Increment the value
The references are copied public void peekAndChange(Rectangle r) { 	print(r.getLength()); print(r.getBreadth); r.setLength(25); r.setBreadth(30); } public void peekCaller() { 		Rectangle r = new Rectangle(); r.setLength(15); r.setBreadth(20); print(r.getLength()); 	print(r.getBreadth); peekAndChange(r); print(r.getLength()); 		print(r.getBreadth); } Passing reference
Passing reference Rectangle Object L=15,B=20 CALLER REFERENCE State changed PEEKANDCHANGE REFERENCE Rectangle Object L=25,B=30
Q&A

More Related Content

What's hot (20)

LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structure
 
Elementary data structure
Elementary data structureElementary data structure
Elementary data structure
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
8 python data structure-1
8 python data structure-18 python data structure-1
8 python data structure-1
 
264finalppt (1)
264finalppt (1)264finalppt (1)
264finalppt (1)
 
Data structure ppt
Data structure pptData structure ppt
Data structure ppt
 
Data Structures 01
Data Structures 01Data Structures 01
Data Structures 01
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
 
arrays
arraysarrays
arrays
 
Sorting
SortingSorting
Sorting
 
Stack & Queue
Stack & QueueStack & Queue
Stack & Queue
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
Data structure
Data structureData structure
Data structure
 
Data Structures (BE)
Data Structures (BE)Data Structures (BE)
Data Structures (BE)
 
An Introduction to the C++ Standard Library
An Introduction to the C++ Standard LibraryAn Introduction to the C++ Standard Library
An Introduction to the C++ Standard Library
 
U nit i data structure-converted
U nit   i data structure-convertedU nit   i data structure-converted
U nit i data structure-converted
 

Viewers also liked

javaMethod.ppt
javaMethod.pptjavaMethod.ppt
javaMethod.pptzahid32
 
Pass by value and pass by reference
Pass by value and pass by reference Pass by value and pass by reference
Pass by value and pass by reference TurnToTech
 
Chap2 class,objects
Chap2 class,objectsChap2 class,objects
Chap2 class,objectsraksharao
 
+2 CS class and objects
+2 CS class and objects+2 CS class and objects
+2 CS class and objectskhaliledapal
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in JavaNiloy Saha
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Asfand Hassan
 
All About ... Functions
All About ... FunctionsAll About ... Functions
All About ... FunctionsMichal Bigos
 
Java Classes methods and inheritance
Java Classes methods and inheritanceJava Classes methods and inheritance
Java Classes methods and inheritanceSrinivas Reddy
 
Chapter 9 & chapter 10 solutions
Chapter 9 & chapter 10 solutionsChapter 9 & chapter 10 solutions
Chapter 9 & chapter 10 solutionsSaeed Iqbal
 
Object and class relationships
Object and class relationshipsObject and class relationships
Object and class relationshipsPooja mittal
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statementsjyoti_lakhani
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in javaRaja Sekhar
 
Overloading in java
Overloading in javaOverloading in java
Overloading in java774474
 

Viewers also liked (20)

javaMethod.ppt
javaMethod.pptjavaMethod.ppt
javaMethod.ppt
 
Pass by value and pass by reference
Pass by value and pass by reference Pass by value and pass by reference
Pass by value and pass by reference
 
Chap2 class,objects
Chap2 class,objectsChap2 class,objects
Chap2 class,objects
 
+2 CS class and objects
+2 CS class and objects+2 CS class and objects
+2 CS class and objects
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)
 
All About ... Functions
All About ... FunctionsAll About ... Functions
All About ... Functions
 
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 method
Class methodClass method
Class method
 
Java Classes methods and inheritance
Java Classes methods and inheritanceJava Classes methods and inheritance
Java Classes methods and inheritance
 
Chapter 9 & chapter 10 solutions
Chapter 9 & chapter 10 solutionsChapter 9 & chapter 10 solutions
Chapter 9 & chapter 10 solutions
 
Object and class relationships
Object and class relationshipsObject and class relationships
Object and class relationships
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
 
Chapter 2 Java Methods
Chapter 2 Java MethodsChapter 2 Java Methods
Chapter 2 Java Methods
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_cParameter passing to_functions_in_c
Parameter passing to_functions_in_c
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in java
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Overloading in java
Overloading in javaOverloading in java
Overloading in java
 

Similar to 08 class and object

Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6sotlsoc
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined MethodPRN USM
 
Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]Palak Sanghani
 
Java methods or Subroutines or Functions
Java methods or Subroutines or FunctionsJava methods or Subroutines or Functions
Java methods or Subroutines or FunctionsKuppusamy P
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Abou Bakr Ashraf
 
Suggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdf
Suggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdfSuggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdf
Suggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdfssuser58be4b1
 
Use Netbeans to copy your last lab (Lab 07) to a new project called La.pdf
Use Netbeans to copy your last lab (Lab 07) to a new project called La.pdfUse Netbeans to copy your last lab (Lab 07) to a new project called La.pdf
Use Netbeans to copy your last lab (Lab 07) to a new project called La.pdfashishgargjaipuri
 
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
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfaromanets
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywordsmanish kumar
 
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++ ExamsMuhammadTalha436
 
Inheritance - Creating a Multilevel Hierarchy In this lab- you will s.pdf
Inheritance - Creating a Multilevel Hierarchy  In this lab- you will s.pdfInheritance - Creating a Multilevel Hierarchy  In this lab- you will s.pdf
Inheritance - Creating a Multilevel Hierarchy In this lab- you will s.pdfEvanpZjSandersony
 
Inheritance - Creating a Multilevel Hierarchy In this lab- you will s.pdf
Inheritance - Creating a Multilevel Hierarchy  In this lab- you will s.pdfInheritance - Creating a Multilevel Hierarchy  In this lab- you will s.pdf
Inheritance - Creating a Multilevel Hierarchy In this lab- you will s.pdfvishalateen
 

Similar to 08 class and object (20)

Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
 
Java Reflection
Java ReflectionJava Reflection
Java Reflection
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined Method
 
Object and class
Object and classObject and class
Object and class
 
Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]Lec 6 14_aug [compatibility mode]
Lec 6 14_aug [compatibility mode]
 
C# p8
C# p8C# p8
C# p8
 
Java generics
Java genericsJava generics
Java generics
 
Java methods or Subroutines or Functions
Java methods or Subroutines or FunctionsJava methods or Subroutines or Functions
Java methods or Subroutines or Functions
 
Generics
GenericsGenerics
Generics
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
Suggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdf
Suggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdfSuggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdf
Suggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdf
 
Use Netbeans to copy your last lab (Lab 07) to a new project called La.pdf
Use Netbeans to copy your last lab (Lab 07) to a new project called La.pdfUse Netbeans to copy your last lab (Lab 07) to a new project called La.pdf
Use Netbeans to copy your last lab (Lab 07) to a new project called La.pdf
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 
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
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Class 10
Class 10Class 10
Class 10
 
Inheritance - Creating a Multilevel Hierarchy In this lab- you will s.pdf
Inheritance - Creating a Multilevel Hierarchy  In this lab- you will s.pdfInheritance - Creating a Multilevel Hierarchy  In this lab- you will s.pdf
Inheritance - Creating a Multilevel Hierarchy In this lab- you will s.pdf
 
Inheritance - Creating a Multilevel Hierarchy In this lab- you will s.pdf
Inheritance - Creating a Multilevel Hierarchy  In this lab- you will s.pdfInheritance - Creating a Multilevel Hierarchy  In this lab- you will s.pdf
Inheritance - Creating a Multilevel Hierarchy In this lab- you will s.pdf
 

More from dhrubo kayal

More from dhrubo kayal (20)

Cipla 20-09-2010
Cipla   20-09-2010Cipla   20-09-2010
Cipla 20-09-2010
 
01 session tracking
01   session tracking01   session tracking
01 session tracking
 
03 handling requests
03 handling requests03 handling requests
03 handling requests
 
02 up close with servlets
02 up close with servlets02 up close with servlets
02 up close with servlets
 
01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup
 
19 reflection
19   reflection19   reflection
19 reflection
 
18 concurrency
18   concurrency18   concurrency
18 concurrency
 
17 exceptions
17   exceptions17   exceptions
17 exceptions
 
16 containers
16   containers16   containers
16 containers
 
15 interfaces
15   interfaces15   interfaces
15 interfaces
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanup
 
13 inheritance
13   inheritance13   inheritance
13 inheritance
 
12 encapsulation
12   encapsulation12   encapsulation
12 encapsulation
 
11 static
11   static11   static
11 static
 
10 access control
10   access control10   access control
10 access control
 
09 packages
09   packages09   packages
09 packages
 
07 flow control
07   flow control07   flow control
07 flow control
 
05 operators
05   operators05   operators
05 operators
 
04 data types & variables
04   data types & variables04   data types & variables
04 data types & variables
 
03 hello world with java
03   hello world with java03   hello world with java
03 hello world with java
 

08 class and object

  • 1. Class and Object DhrubojyotiKayal
  • 2. Class Object Object creation Fields Default values for data members Methods Passing data to methods Agenda
  • 3. Introduces a new type into the system Defines a template for an object Ex – Circle, Rectangle class ATypeName { /* Class body goes here */ } ATypeName a = new ATypeName(); Class Rectangle { } Rectangle rectangle = new Rectangle(); Class
  • 4. Software representation of a real world entity Car, Train, Soccer Created from class templates using new operator Object
  • 5. Objects are manipulated using references Rectangle rectangle = null; Objects must always be created Rectangle rectangle = new Rectangle(); rectangle.draw(); Object creation
  • 6. This is the data member of the class It can be a primitive or any other object reference class DataOnly { inti; double d; boolean b; Rectangle rectangle; } DataOnly data = new DataOnly(); data.i = 47; data.d = 1.1; data.b = false; data.rectangle = new Rectangle(); Access – reference.field == discouraged!!! Fields
  • 7. When a primitive data type is a member of a class, it is guaranteed to get a default value if you do not initialize it Default values for data members
  • 8. The default values are only what Java guarantees when the variable is used as a member of a class. This ensures that member variables of primitive types will always be initialized (something C++ doesn’t do), reducing a source of bugs This guarantee doesn’t apply to local variables—those that are not fields of a class public static void checkthis() { int x; x = x + 1;//will not compile } Default values for data members
  • 9. Function, procedure – a group of statements which manipulate data and optionally return some value. Methods in Java determine the messages an object can receive the name, the arguments, the return type, and the body Method
  • 10. ReturnTypemethodName( /* Argument list */ ) { /* Method body */ } The return type describes the value that comes back from the method after you call it. The argument list gives the types and names for the information that you want to pass into the method. The method name and argument list (which is called the signature of the method) uniquely identify that method objectName.methodName(arg1, arg2, arg3); int x = a.f(); Method
  • 11. public class MethodPlayer { public boolean play(intvol) { System.out.println(“Play volume -” + vol); } public static void main(String a[]) { MethodPlayer player = new MethodPlayer(); player.play(5); } } Method in Action
  • 12. Write a Java program that has one member field integer and one method that tests if a given input integer is odd or even. If the given input is even change the value of the member field with this one Now run a for loop to test the first 100 integers with this method. Add another method to get the value of the member field Exercise
  • 13. A method uses parameters, A called passes arguments You can pass n number of inputs to a method of varying types Java is always pass by value or pass by copy Passing data to methods
  • 14. public booleanisEven(int value) { if(value % 2 ==0) { return true; //increment and print value = value + 1; System.out.println(“Next odd - ” + value); } else{ return false; } } inti = 55; isEven(i); System.out.println(i); Passing primitives
  • 15. Passing primitives 55 int x isEven(i); Copy the value 55 int value 56 Increment the value
  • 16. The references are copied public void peekAndChange(Rectangle r) { print(r.getLength()); print(r.getBreadth); r.setLength(25); r.setBreadth(30); } public void peekCaller() { Rectangle r = new Rectangle(); r.setLength(15); r.setBreadth(20); print(r.getLength()); print(r.getBreadth); peekAndChange(r); print(r.getLength()); print(r.getBreadth); } Passing reference
  • 17. Passing reference Rectangle Object L=15,B=20 CALLER REFERENCE State changed PEEKANDCHANGE REFERENCE Rectangle Object L=25,B=30
  • 18. Q&A