Coming up Constructors Overloading With one parameter

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

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.
CSCI 160 Midterm Review Rasanjalee DM.
Chapter 5 Part 1 COSI 11a Prepared by Ross Shaull.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
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.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
ECE122 Feb. 22, Any question on Vehicle sample code?
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Constructors & Garbage Collection Ch. 9 – Head First Java.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014.
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.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Java Programming Final Keyword In Java. final keyword The final keyword in java is used to restrict the user. The final keyword can be used in many context.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
CLASS AND OBJECTS Valerie Chu, Ph.D. LeMoyne-Owen College 1.
Classes - Intermediate
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
1 CS1110 Thursday, 10 Feb 2011 Discussion of Methods: Executing method calls. If-statements. The return statement in a function. Local variables. For this.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Coming up Which methods are where? – Overriding Calling super’s methods Coupling and cohesion.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Comp1004: Building Better Objects I Methods. Coming up Methods and Parameters – Why Parameterise? – Call by value, call by reference Return Types – Methods.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Content Programming Overview The JVM A brief look at Structure – Class – Method – Statement Magic incantations – main() – output Coding a Dog Programming.
GC211 Data structure Lecture 3 Sara Alhajjam.
Content Programming Overview The JVM A brief look at Structure
Examples of Classes & Objects
CS 302 Week 11 Jim Williams, PhD.
Lesson 2: Building Blocks of Programming
Subtyping Rules David Evans cs205: engineering software BlackBear
Phil Tayco Slide version 1.0 Created Oct 9, 2017
Interfaces and Constructors
Classes & Objects: Examples
Encapsulation and Constructors
Sit next to someone. Today, we do some work in pairs.
Assignment 7 User Defined Classes Part 2
S.VIGNESH Assistant Professor/CSE, SECE
CSE 214 – Computer Science II Stack Applications
JAVA Constructors.
Sit next to someone. Today, we do some work in pairs.
CS 180 Assignment 6 Arrays.
Recap Week 2 and 3.
Sampath Kumar S Assistant Professor, SECE
Constructors under inheritance Variable Shadowing
Barb Ericson Georgia Institute of Technology June 2005
Java Programming Language
More on Creating Classes
Barb Ericson Georgia Institute of Technology Oct 2005
LCC 6310 Computation as an Expressive Medium
Methods/Functions.
References Revisted (Ch 5)
Parameters, Overloading Methods, and Random Garbage
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Corresponds with Chapter 5
Chapter 5 Classes.
Presentation transcript:

Coming up Constructors Overloading With one parameter With two parameters Overloading Constructors and methods

Lecture 5 Objects Ahoy OO

Coming up Constructors Overloading With one parameter With two parameters Overloading Constructors and methods

Constructors OO Look a bit like methods: Arguments No return type public class DVD{ public DVD(){ } Arguments No return type Same name as class

What are they used for? OO The constructor contains the code that is run when you create an object The constructor runs before the object is assigned to a reference It lets you set it up for use

Why not just use set methods? Okay, so lets make a Table (one that you’d eat at) OO

public class Table{ int size; public void sitAt(int numOfPeople){ //Sit some people at the table } public void setSize(int theSize){ size = theSize; ----------------------------------- public class UseTheTable{ public static void main(String[] args){ Table t = new Table(); t.sitAt(8); t.setSize(3); What happens if....

The problem OO The object creation is two step Make the object Set the size If someone else uses your class Will they remember? Or will there be 8 people trying to eat off a coffee table?

It’s worse OO When you declare a data type It defaults to its default value... Numbers default to 0 Boolean defaults to false Objects default to null So the last example would have 8 people seated at a table of size 0!

These constructors seem pretty important.... OO These constructors seem pretty important.... So why haven’t I used/heard of them before? You have. Table t = new Table(); new calls the constructor

But I didn’t write any constructors when I called new! Java helpfully puts in a blank constructor for you.

public class Table{ int size; public void sitAt(int numOfPeople){ //Sit some people at the table } public void setSize(int theSize){ size = theSize; public Table(){ Java’s helpful hidden constructor

So now we know its there... .....We can use it Set the table size to a small table when it is created

public class Table{ int size; public void sitAt(int numOfPeople){ //Sit some people at the table } public void setSize(int theSize){ size = theSize; public Table(){ size = 1;

Or we can pass it an argument....

public class Table{ int size; public void sitAt(int numOfPeople){ //Sit some people at the table } public void setSize(int theSize){ size = theSize; public Table(int sizeToSet){ size = sizeToSet;

Or two arguments...

Uh oh... They have the same name... public class Table{ int size; String wood; public void sitAt(int numOfPeople){ //Sit some people at the table } public void setSize(int theSize){ size = theSize; public Table(int sizeToSet){ size = sizeToSet; public Table(int sizeToSet, String woodType){ wood = woodType; Uh oh... They have the same name...

That can’t work, can it? OO How would the program know which method I wanted to call? They both have the same name Actually it does work You can do the same thing with methods

public void sitAt(int numOfPeople){ numOfSitters = numOfPeople; } public class Table{ int size; String wood; String partyName; int numOfSitters; public void sitAt(int numOfPeople){ numOfSitters = numOfPeople; } public void sitAt(int num, String name){ partyName = name public void setSize(int theSize){ size = theSize; public Table(int sizeToSet){ size = sizeToSet; public Table(int sizeToSet, String woodType){ wood = woodType;

Compiler error! Which method is called? OO If you call someTable.sitAt(4); If you call someTable.sitAt(4, “Martin”); If you call someTable.sitAt(); public void sitAt(int numOfPeople){ numOfSitters = numOfPeople; } public void sitAt(int num, String name){ partyName = name Compiler error!

This is called OO Overloading A method is recognised by its return type and its signature ie the stuff in the brackets, like (int, String)

Coming up Constructors Overloading With one parameter With two parameters Overloading Constructors and methods

Overloading Rules OO Return types can be different As long as the arguments are different too i.e. You can’t just change the return type You can change the access levels to more or less restrictive i.e. You can change public to private, and vice versa Overloading is different to another principle, overiding, which we cover in inheritance.