Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance Inheritance Reserved word protected Reserved word super
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Programming Chapter 3 More about classes. Why?  To make classes easier to find and to use  to avoid naming conflicts  to control access  programmers.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Review of C++ Programming Part II Sheng-Fang Huang.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
Java 程序设计 Java Programming Fall, Contents for Today Java Program Structure  How to Compile a Java Program  How to Run a Java Program Environment.
Chapter 8 More Object Concepts
1 Lecture 2: Object Oriented Programming in Java.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
More Object Concepts Chapter 4.  Our class is made up of several students and each student has a name and test grades  How do we assign the variables.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
1 Java Programming Using Methods, Classes, and Objects.
CIT 590 Intro to Programming First lecture on Java.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
10-Nov-15 Java Object Oriented Programming What is it?
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Objects & Classes Lakshmish Ramaswamy. instanceOf Operator Testing whether an object is of a certain type (class) object instanceOf classname True if.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
More Object Concepts— Farrell, Chapter 4 Dr. Burns.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
By Mr. Muhammad Pervez Akhtar
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Java Packages - allow you to group classes as a collection Standard Java library has packages: java.lang and java.util … Also hierarchical: java and javax.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
JAVA MULTIPLE CHOICE QUESTION.
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Java Programming Language
Object Oriented Programming in java
From C++ to Java Java history: Oak, toaster-ovens, internet language, panacea What it is O-O language, not a hybrid (cf. C++) compiled to byte-code, executed.
Applying OO Concepts Using Java
Object Oriented Programming in java
Classes 5/5 May 14, 2019 ICS102: Classes 5/5.
Presentation transcript:

Applying OO Concepts Using Java

In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten classes Packages and the protected access modifier The finalize() method

Overriding a Method Overriding: If you declare a variable within a class, and use the same variable name within a method of the class, then the variable used inside the method takes precedence, or overrides, the first variable

Overloading a Method Overloading: Involves using one term to indicate diverse meanings Writing multiple methods with the same name, but with different arguments Overloading a Java method means you write multiple methods with a shared name This is polymorphism in action.

Constructors Constructors are a special type of method. Used to create an instance of the class. –e.g. Employee e = new Employee( ); This calls the Employee constructor. Java automatically provides a constructor method when you create a class Programmers can write their own constructor classes Programmers can also write constructors that receive arguments –Such arguments are often used for initialization purposes when values of objects might vary

Overloading Constructors If you create a class from which you instantiate objects, Java automatically provides a constructor But, if you create your own constructor, the automatically created constructor no longer exists As with other methods, you can overload constructors –Overloading constructors provides a way to create objects with or without initial arguments, as needed

Example of Overloading a Constructor public class Employee { public Employee (String n, double a) { name = n; salary = a; } public Employee ( ) { name = “ “; salary = 0; } }

Mutator and Accessor Methods Often referred to as get/set methods. Mutator methods modify fields in a class. –Example: public void setName (String n) { empName = n; } Accessor methods retrieve fields in a class –Example: public String class getName () { return empName; } Be careful not to return mutatable objects in your accessor methods. Why is this important?

Mutator and Accessor Methods Returning mutatable objects in public accessor methods breaks encapsulation!!!! Even if the data element is private, outside classes can now modify it. You should return immutable objects (Strings, ints, etc). If you must return a reference to a mutatable object, you should clone it first. See pg. 112 in book for example of this rogue code.

The finalize() Method Inherited from the Object class. Called by the VM before an object is destroyed and it’s memory is released Use it to release resources that might not otherwise be released (e.g. files) Use it to record the fact that an object has been destroyed

The Import Statement and Using Pre-written Classes The creators of Java wrote nearly 500 classes –For example: System, Character, Boolean, Byte, Short, Integer, Long, Float, and Double are classes These classes are stored in packages, or a library of classes, which is a folder that provides a convenient grouping for classes

java.lang – The package that is implicitly imported into every Java program and contains fundamental classes, or basic classes Fundamental classes include: –System, Character, Boolean, Byte, Short, Integer, Long, Float, and Double Optional classes – Must be explicitly named The Import Statement and Using Pre-written Classes

To use any of the prewritten classes (other than java.lang): –Import the class OR –Import the package which contains the class you are using To import an entire package of classes use the wildcard symbol - * For example: –import java.util.*; //imports all java.util classes –import java.util.Vector; //imports the Vector class –Represents all the classes in a package

Packages Creating packages encourages others to reuse software because it makes it convenient to import many related classes at once Packages are used to: –maintain the uniqueness of class names Using Packages in your programs prevent class name scope conflicts if multiple classes of the same name are used. e.g. both java.util and java.sql have a Date class, so if you are using both packages you need to reference with java.util.Date or java.sql.Date. –group classes to make them more easily accessible to your classes –reference classes in a particular scope What access modifier helps you limit access to packages?

Packages and the Protected Access Modifier the protected access modifier: –Provides you with an intermediate level of security between public and private access –Is used to limit access to classes within the same package –If you create a protected data field or method, it can be used: within its own class in any classes extended from that class or in classes in the same package but it cannot be used by “outside” classes

Putting Your Class in a Package To include your class into a package, use the package statement –The package statement must appear outside the class definition –The package statement looks like this: package ; example: package MC697; class Person {... }

Packages and Directory Structure Packages map to the directory structure. –Example: package com.MC697; public class Test { public static void main (String[] args) { System.out.println(“Testing packages”); } } –This package statement maps to the directory: /com/MC697 where base directory is the directory you are going to compile and execute the class file from

Compiling and Executing Using Packages When using packages you must compile and run from the base directory. So, let’s say c:\temp is the base directory we want to use. The file in the example should be saved to c:\temp\com\MC697\Test.java. To compile: –cd to c:\temp javac com/MC697/Test.java To execute: –cd to c:\temp java com.MC697.Test

Setting the Classpath When using packages or classes outside the java. packages, you need to modify the classpath. What is the classpath? –It is a environment variable that points the jvm to the needed class files. How do you set the classpath? –You can set it using the -classpath option in the javac or java command. This is temporary. You will need to do this every time you use those commands. e.g. javac -classpath c:\user\classdir PackageTest.java java -classpath c:\user\classdir PackageTest –Another way is to modify the environment variable on your machine. e.g. SET CLASSPATH=c:\com\MC697 Different operating systems have different ways of setting this variable. See pg. 137 in book for examples of how to set this on different platforms.

JAR Files Jar files are Java’s version of the zip file. They group packages and class files together in a unit to make it easier to deploy. Can be viewed using Winzip or similar utility. You can point your classpath variable to jar’s that contain needed classes. –e.g. SET CLASSPATH=c:\j2sdk1.4.1\jre\lib\rt.jar;%CLASSPATH%

Javadocs Javadocs are documentation for class files. Javadoc is a utility built into the sdk to automatically build documentation from the java files. –e.g. javadoc VectorDemo.java Remember the /** …. */ documentation symbols? –These are used to denote documentation comments. –Put these before a method or field to include comments about these in the javadocs. –Special tags can @param for methods –See pg. 139 in book for more information