Week 4 Object-based Programming (2) Classes and Objects: A Deeper Look

Slides:



Advertisements
Similar presentations
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Advertisements

1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Composition A whole-part relationship (e.g. Dog-Tail) Whole and part objects have same lifetime –Whole creates instance of part in its constructor In.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
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.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
1 Chapter 3 – Object-Based Programming 2 Initializing Class Objects: Constructors Class constructor is a specical method to initialise instance variables.
Object Oriented Programming
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
1 CS Programming Languages Class 22 November 14, 2000.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Dale Roberts Object Oriented Programming using Java - Getters and Setters Dale Roberts, Lecturer Computer Science, IUPUI
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
CompSci 230 S Programming Techniques
Advanced Programming in Java
Advanced Programming in Java
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Object-Oriented Programming: Classes and Objects
Objects as a programming concept
Agenda Warmup AP Exam Review: Litvin A2
Object Oriented Programming using Java - Class Instance Variables
Chapter 3: Using Methods, Classes, and Objects
11.1 The Concept of Abstraction
Object-Oriented Programming: Classes and Objects
Object Based Programming
Object-Oriented Programming: Interface
Corresponds with Chapter 7
Object-Oriented Programming: Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 9 Object-Oriented Programming: Inheritance
IFS410: Advanced Analysis and Design
Lecture 22 Inheritance Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Classes & Objects: Examples
Classes, Encapsulation, Methods and Constructors (Continued)
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.
Interfaces.
Week 3 Object-based Programming: Classes and Objects
Packages From Deitel & Deitel.
Chapter 11 Inheritance and Polymorphism
The University of Texas – Pan American
Java Inheritance.
Advanced Programming in Java
CS360 Client/Server Programming Using Java
CIS 199 Final Review.
Final and Abstract Classes
Chapter 8 Classes and Objects: A Deeper Look
Classes and Objects Systems Programming.
11.1 The Concept of Abstraction
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Week 4 Object-based Programming (2) Classes and Objects: A Deeper Look MSIS670: Object-Oriented Software Engineering Week 4 Object-based Programming (2) Classes and Objects: A Deeper Look 12/30/2018 4.1

Constructor Class Constructor Same name as class Initializes instance variables of a class object Called when program instantiates an object of that class Class can have several constructors through overloading (next class). When you want to use an object, which is defined by the programmer, a class (object) can call a class (actually class constructor) to instantiate an object of the class. ClassName ref = new ClassName( arguments ); ClassName indicates type of object created, ref is the reference to that object (used as a variable), and new indicates that a new object is created. Arguments specifies constructor argument values. This argument should matches the constructor arguments (number of variables, and each type) – c.f. Overloaded Constructors. e.g., Time1Test line 9, Time1 time = new Time1(); time is a new object of the type “Time1”, with no argument required. Then constructor initializes the object’s instance variables according to the specification of its constructor. 12/30/2018

Implementing a Time Abstract Data Type with a Class: Time2. java (p pp. 366-368 Time2.java defines the class Time Time2Test.java defines the class Time2Test, which is an application. Public classes must be defined in separate files. Every Java class must extend another class. If not extend a class, it implicitly extends Object. 12/30/2018

Using Overloaded Constructors Methods in a class may have the same names. Must have different arguments. Different in number of parameters. Different in data types. e.g., (P. 366-367). public time2(); public time2( int h ); … 12/30/2018

Overriding Methods Method Object.toString Gives String representation of object But we want it to be a standard time format Time2 overrides method toString Lines 97-102 Time2.toString is now different from Object.toString If we remove lines 26-31 of class Time1, Time1 still com[piles correctly. When a message calls for toString method, it uses method java.lang.Object.toString; giving String representation of object (not in a universal time format). 12/30/2018

toString() Method in Time2.java Given a set of parameters (values of variables hour, minute, and second), this method toString gives back a String in a format of hh:mm:ss (in which hh is up to 12 format, not 24 hour format). twoDigits is another object of a class called DecimalFormat, which has a method called format that changes the view of a number into a two digit (even it is a single digit). 12/30/2018

Using set and get methods Mutator method – set public method Allow clients to modify private data Accesor method – get Allow clients to read private data The set and /or get methods are not mandatory for each of the private data. Sometimes it is completely hidden from outside. Only if it makes sense to provide such methods, you should provide them. 12/30/2018

Lab Activities (Week 4) Coding Assignment 4 class MyRectangle Two .java files. Both should be saved on a same project. Similar to the Time2 example. For more advanced: use GUI application to test multiple constructors For this assignment, you have to create a class MyRectangle and another class to test this class. Place them together in a package (project) before you start coding them. Turn in the coding as usual for both files. 12/30/2018