SlideShare une entreprise Scribd logo
1  sur  26
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

Contenu connexe

Tendances

Class & Object - Intro
Class & Object - IntroClass & Object - Intro
Class & Object - IntroPRN USM
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and objectFajar Baskoro
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Majid Saeed
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introductionSohanur63
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++Mohamad Al_hsan
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingSyed Faizan Hassan
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in javaRaja Sekhar
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++NainaKhan28
 

Tendances (18)

Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Class & Object - Intro
Class & Object - IntroClass & Object - Intro
Class & Object - Intro
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Java OO Revisited
Java OO RevisitedJava OO Revisited
Java OO Revisited
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
Inheritance
InheritanceInheritance
Inheritance
 
C++ classes
C++ classesC++ classes
C++ classes
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
 
Class object method constructors in java
Class object method constructors in javaClass object method constructors in java
Class object method constructors in java
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 

Similaire à 9 cm604.14

Pi j2.2 classes
Pi j2.2 classesPi j2.2 classes
Pi j2.2 classesmcollison
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptxakila m
 
Ch 2 Library Classes.pdf
Ch 2 Library Classes.pdfCh 2 Library Classes.pdf
Ch 2 Library Classes.pdfKavitaHegde4
 
Ch 2 Library Classes.pptx
Ch 2 Library Classes.pptxCh 2 Library Classes.pptx
Ch 2 Library Classes.pptxKavitaHegde4
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Mahmoud Alfarra
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaRadhika Talaviya
 
Class and Object.pptx
Class and Object.pptxClass and Object.pptx
Class and Object.pptxHailsh
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...Sagar Verma
 
L5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classL5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classteach4uin
 
Lecture_4-Class and Object.pptx
Lecture_4-Class and Object.pptxLecture_4-Class and Object.pptx
Lecture_4-Class and Object.pptxShahinAhmed49
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
Java Presentation.ppt
Java Presentation.pptJava Presentation.ppt
Java Presentation.pptMorgan309846
 

Similaire à 9 cm604.14 (20)

unit 2 java.pptx
unit 2 java.pptxunit 2 java.pptx
unit 2 java.pptx
 
Pi j2.2 classes
Pi j2.2 classesPi j2.2 classes
Pi j2.2 classes
 
Java
JavaJava
Java
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Java2
Java2Java2
Java2
 
Ch 2 Library Classes.pdf
Ch 2 Library Classes.pdfCh 2 Library Classes.pdf
Ch 2 Library Classes.pdf
 
Ch 2 Library Classes.pptx
Ch 2 Library Classes.pptxCh 2 Library Classes.pptx
Ch 2 Library Classes.pptx
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
 
Class and Object.pptx
Class and Object.pptxClass and Object.pptx
Class and Object.pptx
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
L5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classL5 classes, objects, nested and inner class
L5 classes, objects, nested and inner class
 
Lecture_4-Class and Object.pptx
Lecture_4-Class and Object.pptxLecture_4-Class and Object.pptx
Lecture_4-Class and Object.pptx
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
CS1Lesson13-IntroClasses.pptx
CS1Lesson13-IntroClasses.pptxCS1Lesson13-IntroClasses.pptx
CS1Lesson13-IntroClasses.pptx
 
Java Presentation.ppt
Java Presentation.pptJava Presentation.ppt
Java Presentation.ppt
 
Java sessionnotes
Java sessionnotesJava sessionnotes
Java sessionnotes
 
d.ppt
d.pptd.ppt
d.ppt
 
d.ppt
d.pptd.ppt
d.ppt
 

Plus de myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

9 cm604.14

  • 1. Basics of Classes and Objects 1
  • 2. Objectives On completion of this period, you would be able to learn • Class • General form of class • Object 2
  • 3. Recap • In the previous class we learnt • Language basics like Selection statements Iterative statements Break and continue statements
  • 4. 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
  • 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 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
  • 8. 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
  • 9. General Form Of Class contd.. type methodname2(parameter-list) { (body of method) } --- --- --- --- type methodnameN(parameter-list) { (body of method) } } 9
  • 10. 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
  • 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 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
  • 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 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
  • 18. 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
  • 19. 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
  • 20. Output of Program Area1 = 150 // Through Accessing variables Area2 = 240 // Through accessing methods 20
  • 21. Summary In this class we have discussed • Class • General form of a class • Object 21
  • 22. Quiz 1. Among the following 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