classes & object

41
Classes & Object

Upload: daman-toor

Post on 15-Jul-2015

492 views

Category:

Education


0 download

TRANSCRIPT

Classes & Object

• Represent the real world

BABY

Object oriented programming

• Represent the real world

Baby Name

GenderWeight

Decibels# cry so far

Object oriented programming

• Objects group together Primitives (int, double, char, etc..) Objects (String, etc…)

BabyString name

boolean isMaledouble weight

double decibels int numCry

Object Oriented Programming

• Why not just primitives?

// little baby ram String nameRam;double weightRam;

// little baby Sham String nameSham;double weightSham;

Why use classes?

• Why not just primitives?

// little baby ram String nameRam;double weightRam;

// little baby Sham String nameSham;double weightSham;

// little baby Sham2 String nameSham2;double weightSham2;

Why use classes?

500 Babies? That’s Confusing!!!

Why use classes?

NameGenderWeight

……

BABY1

NameGenderWeight

……

BABY1

NameGenderWeight

……

BABY2

NameGenderWeight

……

BABY3

496 more Babies…..

Baby1 Baby2 Baby3

Nursery

Why use classes?

public class Baby {String name; boolean isMale; double weight; double decibels; int numCry = 0; void cry() { numCry += 1; System.out.println(“Dear mother, ”+ “Please look why I am crying??”);} }

Class -overview

Class Definition

Baby myBaby = new Baby();

Class -overview

Class instance

•Class names are Capitalized

•1 Class = 1 file

•Having a main method means the class can be run

Note

public class CLASSNAME{

CLASSNAME ( ) { } CLASSNAME ([ARGUMENTS]) { }

}

CLASSNAME obj1 = new CLASSNAME();

CLASSNAME obj2 = new CLASSNAME([ARGUMENTS])

Constructors