Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Road Map Introduction to object oriented programming. Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 10 Classes Continued
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Programming Languages and Paradigms Object-Oriented Programming.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
JavaScript, Fourth Edition
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
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.
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.
© 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)
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 3 Introduction to Classes and Objects Definitions Examples.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Introduction to Object-oriented Programming
Topic: Classes and Objects
Inheritance and Polymorphism
Object Based Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 6 Methods: A Deeper Look
Java Programming Language
Packages and Interfaces
Week 6 Object-Oriented Programming (2): Polymorphism
Interfaces.
Applying OO Concepts Using Java
Chapter 7 Objects and Classes
Presentation transcript:

Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming

Introduction to Java Chapter 7 - Classes & Object-oriented Programming2 Classes A class is the basic unit of the Java language. It is the “blueprint” for the objects created from that class. Each class contains some data definitions (called fields), together with methods to manipulate that data. –When the object is instantiated from the class, an instance variable is created for each field in the class. The methods serve as an interface to isolate the data in the class from the outside world.

Introduction to Java Chapter 7 - Classes & Object-oriented Programming3 Class Hierarchy All classes form a part of a class hierarchy. –Classes below a given class are subclasses of the class. –Classes above a given class are superclasses of the class. The class immediately above a given class is known as its immediate superclass. A class inherits both instance variables and methods from it’s immediate superclass. It can add additional variables and methods, and it can override (change) the inherited methods.

Introduction to Java Chapter 7 - Classes & Object-oriented Programming4 Structure of a Class The major components (members) of a class are: –Fields - define the instance variables to be created when an object is instantiated from the class. –Constructors - special methods the define how to initialize variables when an object is instantiated. –Methods - implement the behaviors of a class. –Finalizer - a special method to perform cleanup before an object is destroyed.

Introduction to Java Chapter 7 - Classes & Object-oriented Programming5 The Member Access Operator (.) The members of a class (instance variables and methods) are accessed using the member access operator, or dot operator (.) –To access a member, the user names a reference to a object, followed by the dot operator, and followed by the member name (with no spaces) Examples: Obj.a Access instance variable a Obj.methodA Access method methodA

Introduction to Java Chapter 7 - Classes & Object-oriented Programming6 Example: Timer Class Method resetTimer Class definition Instance variable definition Constructor Method elapsedTime Note: This method does not have a finalizer.

Introduction to Java Chapter 7 - Classes & Object-oriented Programming7 Scope Instance variables have class scope, meaning that they are visible everywhere within a class, including within methods –Example: variable savedTime in class Timer. Local variables within a method have block scope, meaning that they are only visible within the block in which they are defined. –Example: variable eTime in method elapsedTime.

Introduction to Java Chapter 7 - Classes & Object-oriented Programming8 Hidden Instance Variables It is possible for a local variable to have the same name as an instance variable, hiding it within a block. In this case, the hidden instance variable can be accessed using the this reference. Instance variables Local variables Reference hidden instance variables using this

Introduction to Java Chapter 7 - Classes & Object-oriented Programming9 Common Types of Methods Found in a Class Certain type of methods are common to many classes –“ set methods” are used to set the values of instance variables –“ get methods” are used to read the values of instance variables –predicate methods return a true / false result based on some test. –toString method creates a string representation of the contents of the object

Introduction to Java Chapter 7 - Classes & Object-oriented Programming10 Packages A package is a group of classes and methods that share some related purpose. All standard Java classes are grouped in packages, such as java.lang, java.io, etc. To use the contents of any package except java.lang, it must first be imported into a program with an import statement. The import statements must be the first non- comment lines in a program!

Introduction to Java Chapter 7 - Classes & Object-oriented Programming11 Creating Your Own Packages You can create packages for your own methods: –First, add a package statement to each class specifying the package that it belongs to. Place the statement before the class definition: package chapman.testpackage; –Then, compile using the -d option to specify the root directory of the package structure: javac -d d:\packages MyClass.java; –These commands will place file MyClass.class in directory d:\packages\chapman\testpackage.

Introduction to Java Chapter 7 - Classes & Object-oriented Programming12 Example Creating MyClass package statement Compilation statement The class file will now be in directory d:\packages\chapman\testpackage

Introduction to Java Chapter 7 - Classes & Object-oriented Programming13 Using Your Own Packages To use the classes in your packages, you must add an import statement to each class wanting to access the packages. In addition, you must add the root directory of the package structure to the CLASSPATH environment variable. Example: If class chapman.testpackage.MyClass is in directory d:\package\chapman\testpackage, then the root directory of the package structure is d:\package, and it must appear in a CLASSPATH statement.

Introduction to Java Chapter 7 - Classes & Object-oriented Programming14 Example Using MyClass Set class path in environment import statement

Introduction to Java Chapter 7 - Classes & Object-oriented Programming15 Member Access Modifiers Member access modifiers control where a class member can be accessed from: –public : Members can be accessed from any class –private : Members can only be accessed from within the class that they are defined in – : With no modifier, members can be accessed from any class in the same package as the class that they are defined in. –protected : Access from the same package or from any subclass of the class that they are defined in

Introduction to Java Chapter 7 - Classes & Object-oriented Programming16 Finalizers A finalizer is a special method named finalize, which performs any necessary clean-up (releasing resources, etc.) before an object is destroyed. It is automatically called just before an object is destroyed. Most classes do not need a finalizer.

Introduction to Java Chapter 7 - Classes & Object-oriented Programming17 Garbage Collection When an object is no longer needed, it is automatically destroyed by a low-priority thread called the garbage collector. Destroying old objects returns their resources to the system for re-use. Any object that no longer has a reference pointing to it is a candidate for garbage collection. The garbage collector calls a class’s finalizer before destroying it.

Introduction to Java Chapter 7 - Classes & Object-oriented Programming18 Static Variables A static variable is a special type of variable that is shared by all objects instantiated from a class. Static variables are useful for keeping track of global information such as the number of objects instantiated from a class, etc. They are declared with the static keyword: private static int created; // Static! Static variables are also useful for declaring a single shared copy of a constant: static final double C = e8;

Introduction to Java Chapter 7 - Classes & Object-oriented Programming19 Example Class A definition with two instance variables and one static variable x an y are unique in each object, while s is common to both Two objects a1 and a2 instantiated from class A

Introduction to Java Chapter 7 - Classes & Object-oriented Programming20 Static Methods Static methods are methods that can be executed without first instantiating an object of the class containing the method. Static methods can access the static variables in a class, but they cannot access instance variables. Static methods are normally used for utility calculations that are independent of the data in a class, such as sin(), cos(), tan(), etc.

Introduction to Java Chapter 7 - Classes & Object-oriented Programming21 Example: Extended Math Class static methods static variable