Download presentation
Presentation is loading. Please wait.
1
IFS410: Advanced Analysis and Design
Week 3 Object-based Programming: Classes and Objects 11/23/2018 3.1
2
Programming Languages
Procedural Programming Language Action-oriented Functions are units of programming Object-based Programming Language Objects/classes are units Encapsulation and Interfaces Object-oriented Programming Language Other “goodies” – Polymorphism, etc. Object-Oriented Programming Languages (such as Java) encapsulates data (attributes) and methods (behavior) in objects. Objects may allow other objects to communicate their encapsulated members through well-defined interfaces. Inheritance, Polymorphism, and other features that are considered as characteristics of true OOPL, will be discussed in the Chapter 9. In this section, the main focus is creating and using the object, which are enabled by object-based programming languages. 11/23/2018
3
Object-based Programming
How to create objects How to use objects So far, we have learned how to create a Java application. Applets are mentioned briefly. Here, we introduce a third type of a class, which defines a type of objects. A Java application will self-execute by the method “main()”. A Java applet extends JApplet and also self-execute by the methods “init(), start(), and paint()”. This third type of classes will not execute by itself. When called from other objects (methods), this class simply creates an object described in this class, initializes the instance variables by its constructor, and waits for other instructions. 11/23/2018
4
Declaring a Class Fig 3.1: Class Declaration with one method The GradeBook class contains a displayMessage method )lines 7-10) that displays a message on a screen. This class does not have a method “main”. A class that has a main method is a Java application. Such a class is special because the Virtual Machine can use main to begin execution. Class GradeBook is not an application because it does not have main. Therefore, if youtry to execute GradeBook, you will get an error message. 11/23/2018
5
Instantiating an Object of a Class
Fig. 3.2: Creating an Object of class GradeBook and calling its displayMessage method Now, this class, GradeBookTest, is an application, as it has a method “main”. Within the main method, this class is declaring a variable – myGradeBook. Variable myGradeBook is initialized with the result of the class instance creation expression – new GradeBook(). Keyword new creats a new object of the class specified by the “new.” Next, an expression myGradeBook.displayMessage(); Is calling the object’s method – displayMessage – by specifying which object (myGradeBook) and with dot separator (“.”) a name of method that the object should have defined. When executing this application after compiling both files, it will display the message "Welcome to the Grade Book!" 11/23/2018
6
Instance Variables, set Methods and get Methods
Fig. 3.7: GradeBook class that contains a courseName as instance variable This class modifies the Fig. 3.1; not only the “displayMessage()” method, it has two more methods, and a declaration of an instance variable – courseName. Note that the instance variable is outside of any methods, and declared as private. Nobody else but the methods of objects of this class can touch that. In order to assign the value to the instance variable, this class uses the method –setCourseName( String name ) – where, “name” is the value. Also it has a getCourseName() method, which returns a String value of the contents of the variable courseName. Finally, the displayMessage() method will utilize this getCOurseName() method to display the value. 11/23/2018
7
Constructor Class Constructor Same name as class
Initializes instance variables of a class object Called when program instantiates an object of that 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. 11/23/2018
8
Initializing Objects with Constructors
pp 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. 11/23/2018
9
Testing Constructors 11/23/2018
10
Implementing a Time Abstract Data Type with a Class: Time2.java
pp 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. 11/23/2018
11
Time2.java 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. Time2.java lines vs (and others) 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., Time2Test lines 8-13, Time2 t1 = new Time2(); t1 is a new object of the type “Time2”, with no argument required. Then constructor initializes the object’s instance variables according to the specification of its constructor. 11/23/2018
12
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 ). public Time2(); public Time2( int h ); … 11/23/2018
13
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 Time2.toString is now different from Object.toString If we remove lines of class Time2, Time2 still compiles 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). 11/23/2018
14
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). 11/23/2018
15
Controlling Access to Members
Member Access Modifiers Control access to class’ instance variables and methods public Variables and methods are accessible to class clients private Variables and methods not accessible to class clients Accessor / Mutator methods Designers use private data and public methods to enforce the notion of information hiding and the principle of least privilege. By doing so, the programmer of the class controls how the class’ data is manipulated. This is the principle of data encapsulation. It makes easier to debug the program because if there is a problem with data manipulation, the problem is localized in the same class’s methods. 11/23/2018
16
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. 11/23/2018
17
Package Access When no access modifier (public, private, etc.) is provided for a method or variable, it is considered to have package access. When multiple classes are packaged together, these classes can access each other’s package-access methods and data directly from a reference of an object. As we have been seeing in the previous examples, we can use packages that were created before (and stored somewhere) in a class we are creating by importing them. The statement before class declaration, e.g., import java.awt.*, imports the java core package and we can use the public classes and public methods from those packages. We can also create a package by ourselves. Usually a package is a directory and subdirectory (directory com/deitel/jhtp4/ch08 is expressed as com.deitel.jhtp4.ch08) or project in JCreator. Put related classes and interfaces together in a package help programmers manage complexity of application components and facilitate the software reuse. A class file can have only one package statement as the file is stored only in one directory. 11/23/2018
18
Package Access Above figure (Fig on p. 397) shows the example of usage of package access. Notice that this file has two class declarations. One class, PackageDataTest is a public class, but the other, PackageData is not declared as public. Remember that there is only one public class in one file, but you can have many other classes if they are not public. The multiple classes created by one file will be stored in the same package, and thus, they can access to the package access methods in the other classes. 11/23/2018
19
Lab Activities (Week 3) Coding Assignment 3 class MyDate
Two .java files. Both should be saved on a same project. Similar to the GradeBook example. 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. 11/23/2018
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.