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

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

UNIT II DATA TYPES OPERATORS AND CONTROL STATEMENT 1.
Slide 1 Insert your own content. Slide 2 Insert your own content.
1 Classes and Objects in Java Basics of Classes in Java.
Final and Abstract Classes
1 Inheritance Classes and Subclasses Or Extending a Class.
1 Arrays, Strings and Collections [1] Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software Engineering.
1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
Classes and Objects in Java
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified.
1 C++Tutorial Rob Jagnow This tutorial will be best for students who have at least had some exposure to Java or another comparable programming language.
Java Language Quick-Reference Guide B. Oracle10g: Java Programming B - 2 Console Output Java applications and applets can output simple messages to the.
8 Copyright © 2005, Oracle. All rights reserved. Object Life Cycle and Inner Classes.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
0 - 0.
Addition Facts
Color Templates Software Engineering Module: Web UI Programming Topic: HTML TALENTSPRINT | © Copyright 2012.
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Object Oriented Programming with Java
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Chapter 7 Object-Oriented Programming – Additional Details Object Creation - a Detailed Analysis Assigning a Reference Testing Objects For Equality Passing.
1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.
1 CSC 221: Computer Programming I Fall 2006 interacting objects modular design: dot races constants, static fields cascading if-else, logical operators.
5.9 + = 10 a)3.6 b)4.1 c)5.3 Question 1: Good Answer!! Well Done!! = 10 Question 1:
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Phil Campbell London South Bank University Java 1 First Steps.
Twenty C# Questions Explained Gerry O’Brien Content Development Manager Paul Pardi Senior Content Pub Manager.
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Addition 1’s to 20.
Test B, 100 Subtraction Facts
Week 1.
Bottoms Up Factoring. Start with the X-box 3-9 Product Sum
CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Objects and Classes Mostafa Abdallah
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
SEEM Java – Basic Introduction, Classes and Objects.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
Object Oriented Programming I ( ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011.
Methods.
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Topic: Classes and Objects
Java Memory Management
Static data members Constructors and Destructors
Topic: Classes and Objects TALENTSPRINT | © Copyright 2012
JAVA Constructors.
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Presentation transcript:

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

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

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

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.

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

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

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

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

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?

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

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?

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

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

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

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

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

Color Templates TALENTSPRINT | © Copyright Classes and Objects