Class and ObjectDhrubojyotiKayal
ClassObjectObject creationFieldsDefault values for data membersMethodsPassing data to methodsAgenda
Introduces a new type into the systemDefines a template for an objectEx – Circle, Rectangleclass ATypeName { 	/* Class body goes here */ 	}ATypeName a = new ATypeName(); Class Rectangle {}Rectangle rectangle = new Rectangle();Class
Software representation of a real world entityCar, Train, SoccerCreated from class templates using new operatorObject
Objects are manipulated using referencesRectangle rectangle = null;Objects must always be createdRectangle rectangle = new Rectangle();rectangle.draw();Object creation
This is the data member of the classIt can be a primitive or any other object referenceclass 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 itDefault 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 oneNow run a for loop to test the first 100 integers with this method.Add another method to get the value of the member fieldExercise
A method uses parameters, A called passes argumentsYou can pass n number of inputs to a method of varying typesJava is always pass by value or pass by copyPassing 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 primitives55int xisEven(i);Copy the value55int value56Increment the value
The references are copiedpublic 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 referenceRectangle Object L=15,B=20CALLER REFERENCEState changedPEEKANDCHANGE REFERENCERectangle Object L=25,B=30
Q&A

08 class and object

  • 1.
  • 2.
    ClassObjectObject creationFieldsDefault valuesfor data membersMethodsPassing data to methodsAgenda
  • 3.
    Introduces a newtype into the systemDefines a template for an objectEx – Circle, Rectangleclass ATypeName { /* Class body goes here */ }ATypeName a = new ATypeName(); Class Rectangle {}Rectangle rectangle = new Rectangle();Class
  • 4.
    Software representation ofa real world entityCar, Train, SoccerCreated from class templates using new operatorObject
  • 5.
    Objects are manipulatedusing referencesRectangle rectangle = null;Objects must always be createdRectangle rectangle = new Rectangle();rectangle.draw();Object creation
  • 6.
    This is thedata member of the classIt can be a primitive or any other object referenceclass 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 primitivedata type is a member of a class, it is guaranteed to get a default value if you do not initialize itDefault values for data members
  • 8.
    The default valuesare 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( /* Argumentlist */ ) { /* 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 Javaprogram 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 oneNow run a for loop to test the first 100 integers with this method.Add another method to get the value of the member fieldExercise
  • 13.
    A method usesparameters, A called passes argumentsYou can pass n number of inputs to a method of varying typesJava is always pass by value or pass by copyPassing 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 primitives55int xisEven(i);Copythe value55int value56Increment the value
  • 16.
    The references arecopiedpublic 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 referenceRectangle ObjectL=15,B=20CALLER REFERENCEState changedPEEKANDCHANGE REFERENCERectangle Object L=25,B=30
  • 18.