Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012."— Presentation transcript:

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

2 Color Templates TALENTSPRINT | © Copyright 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

3 Color Templates TALENTSPRINT | © Copyright Classes and Objects Using Objects Dice diceOne = new Dice(); diceOne.roll(); int value = diceOne.getFaceValue(); Constructor

4 Color Templates TALENTSPRINT | © Copyright Example : class Car{ public Car(){ // this is constructor... } 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.

5 Color Templates TALENTSPRINT | © Copyright Classes and Objects ProgramJVM 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 Constructor of the Car Class c Name My Car

6 Color Templates TALENTSPRINT | © Copyright Classes and Objects Constructor 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. Declaration Instantiation Initialization

7 Color Templates TALENTSPRINT | © Copyright Classes and Objects ProgramJVM 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? Name My First Car Name My First Car

8 Color Templates TALENTSPRINT | © Copyright Classes and Objects ProgramJVM 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

9 Color Templates TALENTSPRINT | © Copyright Classes and Objects ProgramJVM 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?

10 Color Templates TALENTSPRINT | © Copyright Classes and Objects ProgramJVM 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 c2 NameMy Car

11 Color Templates TALENTSPRINT | © Copyright Classes and Objects ProgramJVM 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 NameMy CarNameMy Car Can a class perform an action on itself?

12 Color Templates TALENTSPRINT | © Copyright 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

13 Color Templates TALENTSPRINT | © Copyright 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; }... }

14 Color Templates TALENTSPRINT | © Copyright 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(); }

15 Color Templates TALENTSPRINT | © Copyright 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(){}

16 Color Templates TALENTSPRINT | © Copyright 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 IT EXERCISE

17 Color Templates TALENTSPRINT | © Copyright Classes and Objects


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

Similar presentations


Ads by Google