Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic: Classes and Objects TALENTSPRINT | © Copyright 2012

Similar presentations


Presentation on theme: "Topic: Classes and Objects TALENTSPRINT | © Copyright 2012"— Presentation transcript:

1 Topic: Classes and Objects TALENTSPRINT | © Copyright 2012
Software Engineering Module: Core of Java Topic: Classes and Objects TALENTSPRINT | © Copyright 2012

2 The content in this presentation is aimed at teaching learners to:
Classes and Objects The content in this presentation is aimed at teaching learners to: Define class and object Differentiate class and object Create simple java classes, construct and use java objects Define Constructor Explain the need for Constructor Create Constructors and Parameterized Constructors Use “this” keyword Instructor Notes: By the end of this course, participants should be able to: <Obj1> <Obj 2>

3 Classes and Objects The Dice Game Player 1 Player 2 Dice 1 Dice 2

4 Classes and Objects What is an object?
An object is some real or conceptual thing. What is interesting about dice? Its face value What does a dice do? It rolls. What happens when a dice rolls? Its face value changes. An object is a self-contained entity that consists of both data (properties) and methods (behaviour ) to manipulate the data.

5 An object if it is of class dice has a face value and rolls.
Classes and Objects Properties & behaviors of Dice Objects Dice 1 has a face value Dice 2 also has a face value . Dice n also has a face value Dice 1 rolls Dice 2 also rolls Dice n also rolls Any dice has: A face value Any dice: Rolls In other words, Any object that is a dice has a face value and rolls. An object if it is of type dice has a face value and rolls. An object if it is of class dice has a face value and rolls.

6 Classes and Objects Classes in Dice Game Player Class Dice Class

7 Classes and Objects What is a Class
A class is a description of a set of objects that share the same attributes and behaviour (operations)

8 Classes and Objects Class vs. abstraction – Class Exercise
Recall the following definitions of abstraction and explain the connect between abstraction and class: Tony Hoare “Abstraction arises from a recognition of similarities between certain objects, situations, or processes in the real world, and the decision to concentrate upon those similarities and to ignore for the time being the differences.” Grady Booch “An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.”

9 Classes and Objects EXERCISE List out the properties & behaviors of Player class. Hint : What is interesting about player? What does a player do?

10 Classes and Objects Properties & Behaviors of Classes in Dice Game
Player Properties name value Properties faceValue Behaviors roll Behaviors throw

11 Classes and Objects Properties & Behaviors of Dice Class class Dice{
int faceValue; void roll(){ //random no. between 1 and 6 faceValue =(int)((Math.random())*10)%5+1; }// end of roll }// end of class Dice Properties faceValue Behaviors roll

12 Classes and Objects Properties & Behaviors of Player Class
class Player{ String name; int value; void throwDice(Dice diceOne, Dice diceTwo){ diceOne.roll(); diceTwo.roll(); value = diceOne.faceValue + diceTwo.faceValue; } }// end of class Player Properties name value Behaviors throwDice

13 Classes and Objects Programmatic definition of Class
A class is a representation for a set of objects that are data abstractions with an interface of named operations (methods) and hidden local state (attributes).

14 Classes and Objects Behavior in an O-O program
In Object-oriented programming, programs are organized as cooperative collections of objects. Each object represents an instance of some class. Objects communicate by passing messages (by calling methods). A message is always given to some object. The response to a message depends upon the class of the Object. All messages have three identifiable parts. playerOne.throwDice(diceOne,diceTwo) 1. Message Receiver 2. Message 3. List of arguments

15 Steps in making objects collaborate with each other:
Classes and Objects Making Objects Collaborate Steps in making objects collaborate with each other: Define Classes Step 1 Create Objects Step 2 Pass Messages Step 3

16 Classes and Objects Dice Game Class and main method class DiceGame{
EXERCISE class DiceGame{ public static void main (String args [] ) { Dice diceOne; diceOne = new Dice(); // create diceTwo object Player playerOne; playerOne = new Player(); playerOne.throwDice(diceOne,diceTwo); // create playerTwo object // ask playerTwo to throwDice if (playerOne.value > playerTwo.value) { System.out.println("Player1 Wins"); } else{ System.out.println("Player2 Wins"); }}// end of main() }//end of class DiceGame Dice diceTwo; diceTwo = new Dice(); Player playerTwo; playerTwo = new Player(); playerTwo.throwDice(diceOne,diceTwo);

17 Classes and Objects Defining a Class Example : class classname {
type instance-variable1; type instance-variable2; // ... type instance-variableN; type methodname1(parameter-list) { // body of method } type methodname2(parameter-list) { type methodnameN(parameter-list) { Example : class Dice{ int faceValue; void roll(){ faceValue = (int)((Math.random())*10)%5 + 1; }// end of roll }// end of class

18 Classes and Objects Attributes Declaration
<modifier> <data type> <name> Modifiers Data Type Name private String ownerName

19 Classes and Objects Method Declaration
<modifier> <return type> <method name> ( <paramaters> ) { <statements> } Modifiers Return Type Method Name Parameter Statements public void setOwnerName String Name ownerName= name;

20 Classes and Objects Creating Objects Example : ClassName refVariable;
refVariable = new Constructor(); or ClassName refVariable = new Constructor(); Example : Dice diceOne ; diceOne = new Dice(); or Dice diceTwo = new Dice();

21 Classes and Objects Using Objects Dice diceOne = new Dice();
diceOne.roll(); int value = diceOne.faceValue();

22 An object is some real or conceptual thing.
Classes and Objects Using Objects There is one more object in the game. It is the diceGame object itself. Because every object in Java has to belong to some class, let's define the DiceGame class. An object is some real or conceptual thing.

23 Classes and Objects Properties & Behaviors of Dice Game Classes
PlayerOne,PlayerTwo,Dice1,Dice2; Properties Play Behaviors class DiceGame{ Player playerOne, playerTwo; Dice Diceone, DiceTwo; void play(){ diceOne = new Dice(); diceTwo = new Dice(); playerOne = new Player(); playerTwo = new Player(); playerOne.throwDice(diceOne,diceTwo); playerTwo.throwDice(diceOne,diceTwo); if (playerOne.value > playerTwo.value) { System.out.println("Player One Wins"); } else{ System.out.println("Player Two Wins"); } }// end of play() }//end of class

24 Classes and Objects EXERCISE Create the main method for the DiceGame class and complete the game.

25 Classes and Objects Using Objects Dice diceOne = new Dice();
diceOne.roll(); int value = diceOne.getFaceValue(); Constructor

26 Classes and Objects Constructor
Is a function that is implicitly invoked when a new object is created. Performs the necessary actions in order to initialize the object. Is a special function with the same name as the class. Example : class Car{ public Car(){ // this is constructor . . . }

27 Classes and Objects Program JVM Memory class Car{ String name;
public Car(){ name=“My Car”; } class MainClass{ public static void main(String args[]){ Car c ; c = new Car(); Stack memory Heap Constructor of the Car Class Name My Car Reference variable (Declaration) c Calls the constructor Allocates memory for the object (for Instantiation)

28 Classes and Objects Constructor
Declaration Associating a variable name with an object type. Instantiation Is done by the new keyword, a JAVA operator that creates the object. Initialization A call to the constructor follows this new operator. This in turn, initializes the new object.

29 Classes and Objects Program JVM Memory class Car{ String name; public Car(){ name=“My First Car”; } class MainClass{ public static void main(String args[]){ Car c1,c2; c1 = new Car(); c2 = new Car(); Stack memory Heap c1 Name My First Car c2 Name My First Car Does it sound meaningful to have the same name for both the cars: “My First Car”?

30 Classes and Objects Parameterized Constructor Program JVM Memory
class Car{ String name; public Car(String carName){ name=carName; } class MainClass{ public static void main(String args[]){ Car c1,c2; c1 = new Car(“My First Car”); c2 = new Car(“My Second Car”); Stack memory Heap c1 Name My First Car c2 Name My Second Car

31 Classes and Objects Program JVM Memory class Car{ String name; public Car(){ name=“My First Car”; } public Car(String carName ){ name=carName; class MainClass{ public static void main(String args[]){ Car c 1,c2; c1 = new Car(); c 2 = new Car(“My Second Car”); Stack memory Heap c1 Name My First Car c2 Name My Second Car It is possible for a class to have more than one constructor?

32 Classes and Objects Program JVM Memory class Car{ String name;
public Car(){ name=“My Car”; } class MainClass{ public static void main(String args[]){ Car c 1,c2; c1 = new Car(); c2 = c1; Stack memory Heap c1 Name My Car c2

33 Classes and Objects Can a class perform an action on itself? Program
JVM Memory class Car{ String name; public Car(){ name=“My Car”; } class MainClass{ public static void main(String args[]){ Car c 1,c2; c 1= new Car(); c 2= new Car(); c2=c1; Stack memory Heap c1 Name My Car c2 Name My Car Can a class  perform an action on itself?

34 Refers to the current object
Classes and Objects Program class Car{ String name; public Car(){ name=“My First Car”; } public Car(String name ){ this.name=name; class MainClass{ public static void main(String args[]){ Car c 1,c2; c1 = new Car(); c 2 = new Car(“My Second Car”); Refers to the current object

35 Classes and Objects “this” keyword
Is a reference to the current object within an instance method or constructor. Explicit constructor invocation -- call another constructor in the same class. Example for explicit constructor invocation : public class MyClass { private int x, y, a, b; public MyClass() { this(0, 0, 0, 0); } public MyClass(int a, int b) { this(0, 0, a, b); public MyClass(int x, int y, int a, int b) { this.x = x; this.y = y; this.a = a; this.b = b; ...

36 Classes and Objects Will this work? Class Test{ // no constructor
int value=10; void display(){ System.out.println(“Value is : ”+value); } class MainClass{ public static void main(String args[]){ Test test = new Test(); // creating object with constructor Test() test.display();

37 What if no constructor is defined in a class ?
Classes and Objects What if no constructor is defined in a class ? At the time of compilation the Java Compiler will insert an empty Definition Of Default Constructor to the java class and then it will compile the program. The empty Definition Of Default Constructor of any class will be as follows: public class-name(){}

38 Classes and Objects Create the following class: TRY IT Class Test{
EXERCISE Create the following class: Class Test{ int sum(int valueOne, int valueTwo){ return valueOne + valueTwo; } Write a main method that instantiates this class, calls sum method and prints the return value. Compile and run the program. TRY IT

39 Classes and Objects


Download ppt "Topic: Classes and Objects TALENTSPRINT | © Copyright 2012"

Similar presentations


Ads by Google