• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
08   class and object
 

08 class and object

on

  • 337 views

 

Statistics

Views

Total Views
337
Views on SlideShare
337
Embed Views
0

Actions

Likes
0
Downloads
5
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    08   class and object 08 class and object Presentation Transcript

    • 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