Basics of Classes and Objects




                                1
Objectives
On completion of this period, you would be able to
learn

• Class
• General form of class
• Object




                                                     2
Recap

• In the previous class we learnt

• Language basics like
     Selection statements
     Iterative statements
     Break and continue statements
Class Fundamentals

WWhat is an entity ?
 • Anything that is uniquely identifiable

 • It has attributes, which describe the entity

 • If we provide methods to such entity

    • We get a class




                                                  4
Class Fundamentals             …contd
Regarding the structure of a class
O How is ‘struct’ in C language designed ?
 • It is a collection of heterogeneous types of data items

 • A list variables are put together

 • If we add methods (functions) to it, we get something
   equivalent to a class




                                                             5
Class Fundamentals            contd

• Class
  • Class is a template for an object
  • It defines a new data type
  • It contains data members and related methods
Examples of class
  • Student
  • Employee
  • Rectangle

                                                   6
General Form Of A Class
•   While defining declare exact form and nature
•   Specify the data and code that operates it
•   Data declared in a class are called ‘instance variables’
•   Simple class contains only code or only data
•   Real world classes contain both code and data
•   Class is declared by keyword ‘class’




                                                               7
General Form Of Class contd..
                          User defined name
Syntax:
class classname {
      type instance-variable1;
      type instance-variable2;
                --- ---
                --- ---
      type instance-variable N;
      type methodname1(parameter-list) {
                (body of method)
          }
                                              8
General Form Of Class contd..

type methodname2(parameter-list) {
          (body of method)
      }
          --- ---
          --- ---
type methodnameN(parameter-list) {
          (body of method)
          }
  }


                                     9
Discussion
• Compare the structure in C with class in Java
  •   Only data members are permitted in struct
  •   Data and methods are permitted in class
  •   Visibility control is not there in struct
  •   Visibility control is available




                                                  10
Example

Class name – Box
Instance variables – width, height and depth
                                 As class defines a type of data,
class Box {                      Here new data type is Box
      double width;
      double height;
      double depth;
      }



                                                                    11
Discussion
• List some more class examples with their data
  and method members
• Product
  – prodId, prodDesc, price, qty
  – receive(), issue() [ in store environment]
• Bike
  – model, regNo, color, cost, milage
  – start(), stop(), accelerate(), applyBreak()



                                                  12
Object – Definition
Object
  • A software bundle of data and the related
  methods
  • Object is an instance of a class
  • Object contains both state and behaviour
     • State means its data members containing
     specific values
     • Behaviour means the collection of methods
     defined in the object




                                                   13
Object – Definition         contd..
• Examples
   • State
     • A student with PIN 004-CM-601
     • An employee with an id EMP608
     • A rectangle with length 15 and width 8

  • Behaviour
     • setMarks(), getMarks(String PIN)
     • computeSalary()
     • computeArea(), setLength(),setWidth()




                                                14
Creating Object
Creating Object
  • There are two steps to create an object
      • Declare object
      • Allocate memory and initialize the object
Syntax for declaring object
  • ClassName objectName;
  • eg. Box myBox;
Syntax for allocate memory and initialize
  • objectName = new ClassName()
  • eg. myBox = new Box();

                                                    15
Creating Object      contd..

Statement           Effect

Box mybox             null
                     mybox

mybox = new Box()
                                        Width
                     mybox
                                        Height
                                        Depth
                                       Box object

                      Fig. 14.1 Creating object



                                                    16
Example On Classes And Objects

class Rectangle {
int length , width;    //declaration of variables
void getData(int x, int y)       //definition of method
{
    length = x;
    width = y;
    }
int rectArea()         //definition of another method
{
    int area = length * width;
                                                          17
Example On Classes And Objects contd..

return(area);
    }
}
class Rectarea              // class with main method
{
public static void main(String args[])
{
int area1,area2;
Rectangle rect1 = new Rectangle(); //creating objects

                                                        18
Accessing Class Members

Rectangle rect2 = new Rectangle();
Rect1.length = 15;                //Accessing variables
Rect1.width = 10;
Area1 = rect1.length * rect1.width;
rect2.getData(20,12);             // Accessing methods
Area2 = rect2.rectArea();
System.out.println( “ Area1 =“ + area1);
System.out.println( “ Area2 =“ + area2);
    }
}
                                                          19
Output of Program




Area1 = 150   // Through   Accessing variables


Area2 = 240   // Through   accessing methods




                                                 20
Summary
In this class we have discussed
    • Class
    • General form of a class
    • Object




                                  21
Quiz
1. Among the following which is keyword to declare
   a class ?
  a)   new
  b)   object
  c)   type
  d)   class




                                                     22
Quiz     Contd..
2 .To access member of a class which operator is
   used ?
 a)   &
 b)   . (dot)
 c)   *
 d)   +




                                                   23
Quiz        Contd..

3. Object is
   a)   Inheritance of a class
   b)   Instance of a class
   c)   Subclass of a class
   d)   Super class of a class




                                           24
Frequently Asked Questions

1. What is a class ?

2. Mention the key word to declare a class

3. What are the common components of a class?

4. What is an object?

5. Write a program in Java for declaring a class and object




                                                              25
swings
                Struts
                 jdbc
              hibernate
                home
  java previous question papers
OCT/NOV-2012 QUESTION PAPER
      April / May 2012 c-09
  October/ November-2011 c-09
       April/ May 2011 c-09
       April/ May 2011 c-05




     Home                         26

9 cm604.14

  • 1.
    Basics of Classesand Objects 1
  • 2.
    Objectives On completion ofthis period, you would be able to learn • Class • General form of class • Object 2
  • 3.
    Recap • In theprevious class we learnt • Language basics like Selection statements Iterative statements Break and continue statements
  • 4.
    Class Fundamentals WWhat isan entity ? • Anything that is uniquely identifiable • It has attributes, which describe the entity • If we provide methods to such entity • We get a class 4
  • 5.
    Class Fundamentals …contd Regarding the structure of a class O How is ‘struct’ in C language designed ? • It is a collection of heterogeneous types of data items • A list variables are put together • If we add methods (functions) to it, we get something equivalent to a class 5
  • 6.
    Class Fundamentals contd • Class • Class is a template for an object • It defines a new data type • It contains data members and related methods Examples of class • Student • Employee • Rectangle 6
  • 7.
    General Form OfA Class • While defining declare exact form and nature • Specify the data and code that operates it • Data declared in a class are called ‘instance variables’ • Simple class contains only code or only data • Real world classes contain both code and data • Class is declared by keyword ‘class’ 7
  • 8.
    General Form OfClass contd.. User defined name Syntax: class classname { type instance-variable1; type instance-variable2; --- --- --- --- type instance-variable N; type methodname1(parameter-list) { (body of method) } 8
  • 9.
    General Form OfClass contd.. type methodname2(parameter-list) { (body of method) } --- --- --- --- type methodnameN(parameter-list) { (body of method) } } 9
  • 10.
    Discussion • Compare thestructure in C with class in Java • Only data members are permitted in struct • Data and methods are permitted in class • Visibility control is not there in struct • Visibility control is available 10
  • 11.
    Example Class name –Box Instance variables – width, height and depth As class defines a type of data, class Box { Here new data type is Box double width; double height; double depth; } 11
  • 12.
    Discussion • List somemore class examples with their data and method members • Product – prodId, prodDesc, price, qty – receive(), issue() [ in store environment] • Bike – model, regNo, color, cost, milage – start(), stop(), accelerate(), applyBreak() 12
  • 13.
    Object – Definition Object • A software bundle of data and the related methods • Object is an instance of a class • Object contains both state and behaviour • State means its data members containing specific values • Behaviour means the collection of methods defined in the object 13
  • 14.
    Object – Definition contd.. • Examples • State • A student with PIN 004-CM-601 • An employee with an id EMP608 • A rectangle with length 15 and width 8 • Behaviour • setMarks(), getMarks(String PIN) • computeSalary() • computeArea(), setLength(),setWidth() 14
  • 15.
    Creating Object Creating Object • There are two steps to create an object • Declare object • Allocate memory and initialize the object Syntax for declaring object • ClassName objectName; • eg. Box myBox; Syntax for allocate memory and initialize • objectName = new ClassName() • eg. myBox = new Box(); 15
  • 16.
    Creating Object contd.. Statement Effect Box mybox null mybox mybox = new Box() Width mybox Height Depth Box object Fig. 14.1 Creating object 16
  • 17.
    Example On ClassesAnd Objects class Rectangle { int length , width; //declaration of variables void getData(int x, int y) //definition of method { length = x; width = y; } int rectArea() //definition of another method { int area = length * width; 17
  • 18.
    Example On ClassesAnd Objects contd.. return(area); } } class Rectarea // class with main method { public static void main(String args[]) { int area1,area2; Rectangle rect1 = new Rectangle(); //creating objects 18
  • 19.
    Accessing Class Members Rectanglerect2 = new Rectangle(); Rect1.length = 15; //Accessing variables Rect1.width = 10; Area1 = rect1.length * rect1.width; rect2.getData(20,12); // Accessing methods Area2 = rect2.rectArea(); System.out.println( “ Area1 =“ + area1); System.out.println( “ Area2 =“ + area2); } } 19
  • 20.
    Output of Program Area1= 150 // Through Accessing variables Area2 = 240 // Through accessing methods 20
  • 21.
    Summary In this classwe have discussed • Class • General form of a class • Object 21
  • 22.
    Quiz 1. Among thefollowing which is keyword to declare a class ? a) new b) object c) type d) class 22
  • 23.
    Quiz Contd.. 2 .To access member of a class which operator is used ? a) & b) . (dot) c) * d) + 23
  • 24.
    Quiz Contd.. 3. Object is a) Inheritance of a class b) Instance of a class c) Subclass of a class d) Super class of a class 24
  • 25.
    Frequently Asked Questions 1.What is a class ? 2. Mention the key word to declare a class 3. What are the common components of a class? 4. What is an object? 5. Write a program in Java for declaring a class and object 25
  • 26.
    swings Struts jdbc hibernate home java previous question papers OCT/NOV-2012 QUESTION PAPER April / May 2012 c-09 October/ November-2011 c-09 April/ May 2011 c-09 April/ May 2011 c-05 Home 26