Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Section 5 – Classes. Object-Oriented Language Features Abstraction –Abstract or identify the objects involved in the problem Encapsulation –Packaging.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
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,
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CSE 115 Week 4 February 4 - 8, Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday.
1 More on Classes Overview l Overloading l The this keyword l The toString method l The equals method.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Classes. Class expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
UML Basics & Access Modifier
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Introducing Objects. Structure  Objects have two parts: Instance Variables (attributes, adjectives) Instance Variables (attributes, adjectives) private.
Agenda Object Oriented Programming Reading: Chapter 14.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
ECE122 Feb. 22, Any question on Vehicle sample code?
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
10-Nov-15 Java Object Oriented Programming What is it?
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Objects and Classes Mostafa Abdallah
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
More on Objects Mehdi Einali Advanced Programming in Java 1.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
CLASS AND OBJECTS Valerie Chu, Ph.D. LeMoyne-Owen College 1.
Non-Static Classes What is the Object of this lecture?
Classes - Intermediate
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Topics Instance variables, set and get methods Encapsulation
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Class Fundamentals BCIS 3680 Enterprise Programming.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
1 More About Derived Classes and Inheritance Chapter 9.
Introduction to Constructors Lecture # 4. Copyright © 2011 Pearson Education, Inc. 3-2 Arguments Passed By Value In Java, all arguments to a method are.
Modern Programming Tools And Techniques-I
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Some Eclipse shortcuts
Static Methods 14-Nov-18.
An Introduction to Java – Part II
© A+ Computer Science - OOP © A+ Computer Science -
Today’s topics UML Diagramming review of terms
Classes Lecture 7 from Chapter /1/11.
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)
JAVA CLASSES.
Class.
Java Programming Language
Chapter 4 Constructors Section 4.4
Object-Oriented Design AND CLASS PROPERTIES
Object-Oriented Design AND CLASS PROPERTIES
Day 11 The Last Week!.
Object-Oriented Design AND CLASS PROPERTIES
Presentation transcript:

Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an object. The constructor name always matches the class name -- exactly!

Default Constructor Java always provides a default constructor in every class. You do not have to declare one. It provides parameter-less default constructor that will initialize each instance variable to a default value. double – 0.0 int 0 boolean – false String – null A default constructor would look like below: public Dog() { } public Turtle() {} public SuperHero() {}

Creating your own Constructors Visibility is always public so you can create an object. Example below public class Dog public Dog() { } public class Turtle public Turtle() { } Format for default constructor: visibility ClassName () { }

Initializing Data in the Constructor Default constructors could also be used to initialize a value to instance variables Public class Name { private String name; public Name() { name = “My name”; }

When you create a constructor – you lose the default Remember Java does provide a default constructor if you do not put a constructor in your program. If you type a constructor in your program Java will no longer provide the default. Therefore when you create overloaded constructors for your program, you will need to also create the default if you need that format.

Overloaded Constructor Format: visibility ClassName(dataType variableName) When you create an overloaded constructor the data being passed through the constructor will be used to initialize the instance variables. You will create variables that will be passed through the parameter that will then initialize the instance variables. Example on next slide.

public class SuperHero { private String name; private String power; private int speed; private String weapon; public SuperHero(String n, String p, int s, String w) { name = n; power = p; seed = s ; weapon = w; } Let’s create a SuperHero class. SuperHeros usually all have a name, power, speed, weapon, however this information is different for each SuperHero object created. The information passed through the parameter will initialize the instance variables.

Creating an object from a default constructor When the object is created you always use the keyword new. Creating an object from a default constructor: ClassName object = new ClassName(); Dog d = new Dog(); Turtle t = new Turtle(); Bug b = new Bug();

Creating object from an overloaded constructor The overloaded constructor requires that data be passed when the object is created. It must be passed in the exact order listed in the parameter. public SuperHero(String n, String p, int s, String w) { name = n; power = p; seed = s ; weapon = w; } SuperHero s = new SuperHero(“Iron Man”, “Suit”, 400, “Repulsor rays”

Practice Constructor Object ____________________ public Dog() Dog d = new Dog(); public Dog(String name) Dog d = new Dog(“Lassie”); public Rectangle(double width, double height) Rectangle r = new Rectangle(15, 20);

Summary Java provides a default constructor for every class. If you create a constructor you will lose the default Constructors are always public Constructors do no return anything Constructors always start with a curly brace and end with a curly brace Default constructors have an empty parameter Overloaded constructors pass information through the parameter You must declare the datatype and the variable name in the parameter for an overloaded constructor The keyword for creating an object is new When you create an object from an overloaded constructor you pass information through the parameter in the order specified by the constructor.