9 cm604.14
Upcoming SlideShare
Loading in...5
×
 

9 cm604.14

on

  • 217 views

 

Statistics

Views

Total Views
217
Views on SlideShare
201
Embed Views
16

Actions

Likes
0
Downloads
2
Comments
0

1 Embed 16

http://improvejava.blogspot.in 16

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

9 cm604.14 9 cm604.14 Presentation Transcript

  • Basics of Classes and Objects 1
  • ObjectivesOn completion of this period, you would be able tolearn• 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 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
  • 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
  • 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
  • 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 nameSyntax: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
  • 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
  • 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 – 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
  • 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 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
  • Creating Object contd..Statement EffectBox mybox null myboxmybox = new Box() Width mybox Height Depth Box object Fig. 14.1 Creating object 16
  • 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
  • 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 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
  • Output of ProgramArea1 = 150 // Through Accessing variablesArea2 = 240 // Through accessing methods 20
  • SummaryIn this class we have discussed • Class • General form of a class • Object 21
  • Quiz1. 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 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
  • 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