08 class and object


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

08 class and object

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