Fundamentals of Software Development 1Slide 1 Recap: Constructors, the new operator and the this object Three ideas:Three ideas: –How to write a constructor.

Slides:



Advertisements
Similar presentations
JAVA Revision Lecture Electronic Voting System Marina De Vos.
Advertisements

OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
Inheritance Writing and using Classes effectively.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
UniS 1 Object-Oriented Software Engineering CS288.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Fundamentals of Software Development 1Slide 1 Why have interfaces? A Java interface is the “face” that one class shows othersA Java interface is the “face”
Fundamentals of Software Development 1Slide 1 Today’s Summary UML class diagrams – –Why classes are important – –UML class diagrams – relationships – –UML.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Fundamentals of Software Development 1Slide 1 One class can describe many different instancesOne class can describe many different instances –Two NameDroppers,
Fundamentals of Software Development ISlide 1 Recap: Key ideas in WordGames ClassClass –versus instances –Defining –use of fields –Constructors E.g., to.
Fundamentals of Software Development 1Slide 1 Interfaces Outline:Outline: –Generic interface versus Java interface –What a (Java) interface is: Its syntaxIts.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Fundamentals of Software Development 1Slide 1 Method invocation versus definition To define (write) a method means to write its codeTo define (write) a.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
OOPDA Review. Terminology class-A template defining state and behavior class-A template defining state and behavior object-An instance of a class object-An.
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.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 8 More Object Concepts
Intro to OOP with Java, C. Thomas Wu
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Fundamentals of Software Development 1Slide 1 Recap: Key ideas in WordGames Defining a classDefining a class Extending a class versus implementing an interfaceExtending.
CS 2430 Day 9. Announcements Quiz on Friday, 9/28 Prog1: see , see me as soon as possible with questions/concerns Prog2: do not add any public methods.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
AP Computer Science A – Healdsburg High School 1 Unit 3 - Objects and Classes Lab: First Steps - Example.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CET203 SOFTWARE DEVELOPMENT Session 2B Constructors, Overriding and Overloading.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
Fundamentals of Software Development 1Slide 1 Correcting Run-Time Errors Small changes and the “What did you just change?” ruleSmall changes and the “What.
Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how.
1 Object-Oriented Software Engineering CS Java OO Fundamentals Contents Classes and Objects Making new objects Method declarations Encapsulating.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
 Implementing a class ◦ Implementing an interface ◦ Using documented stubs before coding ◦ Writing JUnit tests before coding ◦ Using fields CSSE 220 Object-Oriented.
CS100A, Fall Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
 We have created our two separate 'person' objects, we can set their properties using the methods (the setters) we created.  class person {  var $name;
Lecture 3: More on Classes Textbook: Chapter 2 Recap: –Classes vs. Objects –Q: What comes first a class or an object? –Q: Do we need a class to create.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Fundamentals of Software Development 1Slide 1 Inheritance Key ideas of Inheritance:Key ideas of Inheritance: –Overriding methods –Protected visibility.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Fundamentals of Software Development 1Slide 1 Today’s Summary InterfacesInterfaces –What are they? –Why are they important? –How do they relate to WordGames?
Fundamentals of Software Development 1Slide 1 Recap: Key ideas in BallWorlds so far Writing a class:Writing a class: –structure –process Writing methods:Writing.
Recap: Key ideas in WordGames
Recap: The design of Word Games
Objects as a programming concept
Objects as a programming concept
Memberwise Assignment / Initialization
وقُلِ اعمَلُوا فَسَيرَى الله عَمَلَكُم وَرَسُولَهُ والمُؤمِنُون
group work #hifiTeam
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
Java LESSON 7 Objects, Part 1
Class Inheritance (Cont.)
An Introduction to Java – Part II
CSC 113: Computer programming II
CS/ENGRD 2110 Fall 2017 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2016 Lecture 5: Local vars; Inside-out rule; constructors
ITunes Lab Copyright © 2012 Pearson Education, Inc.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises UTPA – Fall 2012 This set of slides is revised from.
CLASS DEFINITION (FIELDS)
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; 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)
Fundamentals of Software Development 1
Presentation transcript:

Fundamentals of Software Development 1Slide 1 Recap: Constructors, the new operator and the this object Three ideas:Three ideas: –How to write a constructor –How to use a constructor with the new operator And why to use oneAnd why to use one –What the this object is The next several slides review each of these ideas

Fundamentals of Software Development 1Slide 2 Writing Constructors public class NameDropper extends StringTransformer implements StringTransformable { extends StringTransformer implements StringTransformable { private String name; private String name; public NameDropper () { public NameDropper () { this.name = "Who knows" ; } public NameDropper(String givenName) { public NameDropper(String givenName) { this.name = givenName; this.name = givenName; } public NameDropper (int givenNumber) { public NameDropper (int givenNumber) { this.name = "Car " + GivenNumber; this.name = "Car " + GivenNumber; } public String transform(String x) { public String transform(String x) { return this.name + " says " + x; }} A class can have many constructors, each with its own footprint (order and types of parameters) This constructor can be used to initialize the name field with the given name This constructor can be used to initialize the name field to the default value of “Who knows” This constructor can be used to initialize the name field as “Car number” Questions?

Fundamentals of Software Development 1Slide 3 Using Constructors public class NameDropper extends StringTransformer implements StringTransformable { extends StringTransformer implements StringTransformable { private String name; private String name; public NameDropper () { public NameDropper () { this.name = "Who knows" ; } public NameDropper(String givenName) { public NameDropper(String givenName) { this.name = givenName; this.name = givenName; } public NameDropper (int givenNumber) { public NameDropper (int givenNumber) { this.name = "Car " + GivenNumber; this.name = "Car " + GivenNumber; } public String transform(String x) { public String transform(String x) { return this.name + " says " + x; }} x2 = new NameDropper("Ali"); x2.transform("Hi") yields "Ali says Hi" x1 = new NameDropper(); x1.transform("Hi") yields "Who knows says Hi" x3 = new NameDropper(54); x3.transform("Hi") yields "Car 54 says Hi" NameDropper x1, x2, x3; Write 3 statements that construct x1.. x3 using the 3 constructors, respectively. Questions?

Fundamentals of Software Development 1Slide 4 The this object public class NameDropper extends StringTransformer implements StringTransformable { extends StringTransformer implements StringTransformable { private String name; private String name; public NameDropper () { public NameDropper () { this.name = "Who knows" ; } public NameDropper(String givenName) { public NameDropper(String givenName) { this.name = givenName; this.name = givenName; } public NameDropper (int givenNumber) { public NameDropper (int givenNumber) { this.name = "Car " + GivenNumber; this.name = "Car " + GivenNumber; } public String transform(String x) { public String transform(String x) { return this.name + " says " + x; }} x1 = new NameDropper(); x2 = new NameDropper("Ali"); x3 = new NameDropper(54); During execution of: x1.transform("Hi") the keyword this in NameDropper refers to the same object to which x1 refers. During execution of: x2.transform("Hi") the keyword this in NameDropper refers to the same object to which x2 refers. And so forth. Questions about what the keyword this means?