Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Basics of Classes and Objects                                1
ObjectivesOn completion of this period, you would be able tolearn• Class• General form of class• Object                   ...
Recap• In the previous class we learnt• Language basics like     Selection statements     Iterative statements     Break a...
Class FundamentalsWWhat is an entity ? • Anything that is uniquely identifiable • It has attributes, which describe the en...
Class Fundamentals             …contdRegarding the structure of a classO How is ‘struct’ in C language designed ? • It is ...
Class Fundamentals            contd• Class  • Class is a template for an object  • It defines a new data type  • It contai...
General Form Of A Class•   While defining declare exact form and nature•   Specify the data and code that operates it•   D...
General Form Of Class contd..                          User defined nameSyntax:class classname {      type instance-variab...
General Form Of Class contd..type methodname2(parameter-list) {          (body of method)      }          --- ---         ...
Discussion• Compare the structure in C with class in Java  •   Only data members are permitted in struct  •   Data and met...
ExampleClass name – BoxInstance variables – width, height and depth                                 As class defines a typ...
Discussion• List some more class examples with their data  and method members• Product  – prodId, prodDesc, price, qty  – ...
Object – DefinitionObject  • A software bundle of data and the related  methods  • Object is an instance of a class  • Obj...
Object – Definition         contd..• Examples   • State     • A student with PIN 004-CM-601     • An employee with an id E...
Creating ObjectCreating Object  • There are two steps to create an object      • Declare object      • Allocate memory and...
Creating Object      contd..Statement           EffectBox mybox             null                     myboxmybox = new Box(...
Example On Classes And Objectsclass Rectangle {int length , width;    //declaration of variablesvoid getData(int x, int y)...
Example On Classes And Objects contd..return(area);    }}class Rectarea              // class with main method{public stat...
Accessing Class MembersRectangle rect2 = new Rectangle();Rect1.length = 15;                //Accessing variablesRect1.widt...
Output of ProgramArea1 = 150   // Through   Accessing variablesArea2 = 240   // Through   accessing methods               ...
SummaryIn this class we have discussed    • Class    • General form of a class    • Object                                ...
Quiz1. Among the following which is keyword to declare   a class ?  a)   new  b)   object  c)   type  d)   class          ...
Quiz     Contd..2 .To access member of a class which operator is   used ? a)   & b)   . (dot) c)   * d)   +               ...
Quiz        Contd..3. Object is   a)   Inheritance of a class   b)   Instance of a class   c)   Subclass of a class   d)  ...
Frequently Asked Questions1. What is a class ?2. Mention the key word to declare a class3. What are the common components ...
swings                Struts                 jdbc              hibernate                home  java previous question paper...
Upcoming SlideShare
Loading in …5
×

9 cm604.14

441 views

Published on

  • Be the first to comment

  • Be the first to like this

9 cm604.14

  1. 1. Basics of Classes and Objects 1
  2. 2. ObjectivesOn completion of this period, you would be able tolearn• Class• General form of class• Object 2
  3. 3. Recap• In the previous class we learnt• Language basics like Selection statements Iterative statements Break and continue statements
  4. 4. Class FundamentalsWWhat 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. 5. Class Fundamentals …contdRegarding the structure of a classO 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. 6. Class Fundamentals contd• Class • Class is a template for an object • It defines a new data type • It contains data members and related methodsExamples of class • Student • Employee • Rectangle 6
  7. 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. 8. General Form Of Class contd.. User defined nameSyntax:class classname { type instance-variable1; type instance-variable2; --- --- --- --- type instance-variable N; type methodname1(parameter-list) { (body of method) } 8
  9. 9. General Form Of Class contd..type methodname2(parameter-list) { (body of method) } --- --- --- ---type methodnameN(parameter-list) { (body of method) } } 9
  10. 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. 11. ExampleClass name – BoxInstance 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. 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. 13. Object – DefinitionObject • 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. 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. 15. Creating ObjectCreating Object • There are two steps to create an object • Declare object • Allocate memory and initialize the objectSyntax for declaring object • ClassName objectName; • eg. Box myBox;Syntax for allocate memory and initialize • objectName = new ClassName() • eg. myBox = new Box(); 15
  16. 16. Creating Object contd..Statement EffectBox mybox null myboxmybox = new Box() Width mybox Height Depth Box object Fig. 14.1 Creating object 16
  17. 17. Example On Classes And Objectsclass Rectangle {int length , width; //declaration of variablesvoid getData(int x, int y) //definition of method{ length = x; width = y; }int rectArea() //definition of another method{ int area = length * width; 17
  18. 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. 19. Accessing Class MembersRectangle rect2 = new Rectangle();Rect1.length = 15; //Accessing variablesRect1.width = 10;Area1 = rect1.length * rect1.width;rect2.getData(20,12); // Accessing methodsArea2 = rect2.rectArea();System.out.println( “ Area1 =“ + area1);System.out.println( “ Area2 =“ + area2); }} 19
  20. 20. Output of ProgramArea1 = 150 // Through Accessing variablesArea2 = 240 // Through accessing methods 20
  21. 21. SummaryIn this class we have discussed • Class • General form of a class • Object 21
  22. 22. Quiz1. Among the following which is keyword to declare a class ? a) new b) object c) type d) class 22
  23. 23. Quiz Contd..2 .To access member of a class which operator is used ? a) & b) . (dot) c) * d) + 23
  24. 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. 25. Frequently Asked Questions1. What is a class ?2. Mention the key word to declare a class3. 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. 26. swings Struts jdbc hibernate home java previous question papersOCT/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

×