17
Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

Embed Size (px)

Citation preview

Page 1: Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

Software Engineering

Module: Core of Java Topic: Constructors

TALENTSPRINT | © Copyright 2012

Page 2: Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

TALENTSPRINT | © Copyright 2012 2

The content in this presentation is aimed at teaching learners to:

Classes and Objects

• Define Constructor

• Explain the need for Constructor

• Create Constructors and Parameterized Constructors

• Use “this” keyword

Page 3: Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

TALENTSPRINT | © Copyright 2012 3

Classes and Objects

Using Objects

Dice diceOne = new Dice();

diceOne.roll();

int value =

diceOne.getFaceValue(); Constructor

Page 4: Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

TALENTSPRINT | © Copyright 2012 4

Example : class Car{

public Car(){ // this is constructor. . . }

}

Classes and Objects

ConstructorConstructor

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.

Page 5: Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

TALENTSPRINT | © Copyright 2012 5

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 memory

Reference variable (Declaration)

Reference variable (Declaration)

Allocates memory for the object

(for Instantiation)

Allocates memory for the object

(for Instantiation)

Calls the constructor

Calls the constructor

Constructor of the Car ClassConstructor of the Car Class

c

Name My Car

Page 6: Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

TALENTSPRINT | © Copyright 2012 6

Classes and Objects

ConstructorConstructor

Associating a variable name with an object type.

Is done by the new keyword, a JAVA operator that creates the object.

A call to the constructor follows this new operator. This in turn, initializes the new object.

DeclarationDeclaration

InstantiationInstantiation

InitializationInitialization

Page 7: Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

TALENTSPRINT | © Copyright 2012 7

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 memory

c1

c2

Does it sound meaningful to have the same name for both the cars: “My First Car”?

Does it sound meaningful to have the same name for both the cars: “My First Car”?

Name My First Car

Name My First Car

Page 8: Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

TALENTSPRINT | © Copyright 2012 8

Classes and Objects

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 memory

c1

c2

Parameterized Constructor

Name My Second Car

Name My First Car

Page 9: Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

TALENTSPRINT | © Copyright 2012 9

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 memory

c1

c2 Name My Second Car

Name My First Car

It is possible for a class to have more than one constructor? It is possible for a class to have more than one constructor?

Page 10: Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

TALENTSPRINT | © Copyright 2012 10

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 memory

c1

c2Name My Car

Page 11: Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

TALENTSPRINT | © Copyright 2012 11

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; c 1= new Car(); c 2= new Car(); c2=c1; }}

Stack memory

Heap memory

c1

c2

Name My Car

Name My Car

Can a class  perform an action on itself?Can a class  perform an action on itself?

Page 12: Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

TALENTSPRINT | © Copyright 2012 12

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 Refers to the

current object

Page 13: Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

TALENTSPRINT | © Copyright 2012 13

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; } ... }

Page 14: Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

TALENTSPRINT | © Copyright 2012 14

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(); }}

Page 15: Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

TALENTSPRINT | © Copyright 2012 15

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(){}

Page 16: Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

TALENTSPRINT | © Copyright 2012 16

Classes and Objects

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 ITEXERCISE

Page 17: Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012

TALENTSPRINT | © Copyright 2012 17

Classes and Objects