Web Programming with Java Java - Object-Oriented Programming doc

52 313 0
Web Programming with Java Java - Object-Oriented Programming doc

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

1 Web Programming with Java Java Object-Oriented Programming Huynh Huu Viet Email: viethh@uit.edu.vn Department of Information Systems 2008 © Department of Information Systems - University of Information Technology 2 Outline Objects and Classes OOP Concepts  Encapsulation  Aggregation/Composition  Inheritance & Polymorphism Some useful Java classes Exception Handling, I/O Readings & Exercise Discussion 2008 © Department of Information Systems - University of Information Technology 3 Objects and Classes Basic OO concepts  Object, class, state, behavior, message Creating Java classes, objects, properties and methods Constructors Package Reference data type Variable scope 2008 © Department of Information Systems - University of Information Technology 4 Basic OO concepts : Object  Object: any thing, which can be described by  State: property, or attribute  Behavior: function/action, or method: ask the object to perform a task  Example  A student, a desk, a circle, a button, and even a loan 2008 © Department of Information Systems - University of Information Technology 5 Basic OO concepts : Class  Class: a set of objects with common attributes and behaviors  An object is an instance of a class  Class is a template or blueprint that defines what an object's data and methods will be 2008 © Department of Information Systems - University of Information Technology 6 Basic OO concepts : Message Objects interact (communicate) by passing messages 2008 © Department of Information Systems - University of Information Technology 7 Object-Oriented Programming  Class/object is the focus of OOP  At design time  Class is the basic programming unit; a program consists of one or more classes  Programming focuses on • Defining classes and their properties • Manipulating classes/objects properties and behaviors • Handling objects interactions  At execution time  Programs are executed through predefined manipulations and interactions 2008 © Department of Information Systems - University of Information Technology 8 Constructor  Constructor is a special method used for object creation  Circle myCircle = new Circle();  Default constructor  A constructor without any parameter  If a programmer doesn’t define any constructor for a class, JRE will implicitly create a default constructor 2008 © Department of Information Systems - University of Information Technology 9 Using Constructor  Defining constructors  Using constructor for default states and behaviors of an object when it is initially created Three differences with methods:  Must have the same name as the class itself.  Do not have a return type - not even void.  Invoked using the new operator when an object is created. ClassName() //no “void” or any other data type { … } 2008 © Department of Information Systems - University of Information Technology 10 Constructor Overloading Like methods, constructors can be overloaded This offers greater flexibility and convenience of creating objects [...]... Information Technology 14 Java Packages Java hierarchically organizes classes into packages java. lang java. text java. util … Classes need to be referred using its complete name (package + class name): for example, java. util.Calendar Packages can be “imported” to avoid writing package names every time you use a class (except java. lang) import java. util.*; 2008 © Department of Information Systems - University of... outside all methods Used within the class • Can be used/referenced anywhere directly Used with objects • Using the "." operator and preceded with object name • Ex: myCircle.radius 2008 © Department of Information Systems - University of Information Technology 11 Using Objects: using methods Defining methods With a return value (type) Without a return value (type) – void Calling Methods Within the class: Called... Information Systems - University of Information Technology 23 Access Modifiers Encapsulation is implemented through the use of access modifiers/specifiers private - completely encapsulated within the class private int length; default (no modifier) - within the same package int length; public - completely open public int length; (protected) 2008 © Department of Information Systems - University of Information... vn.edu.uit .java 4web; package vn.edu.uit.assignment; package vn.edu.uit.lecture .web; 2008 © Department of Information Systems - University of Information Technology 16 Default Package A class without any package defined is in a “default package” The default package is NOT the root package! Classes in the default package cannot be referenced outside the default package 2008 © Department of Information Systems -. .. of Information Systems - University of Information Technology 27 Aggregation Implementation Aggregation is very common in object modeling and Java programs In Java, this relationship is implemented as a class member variable is of a class/object type, rather than primitive type 2008 © Department of Information Systems - University of Information Technology 28 Modeling One-to-One Class A has a class... referenced only by those in the same package class Book { … } Note: any java source file can have only one public class 2008 © Department of Information Systems - University of Information Technology 25 Java Access Modifiers 2008 © Department of Information Systems - University of Information Technology 26 Aggregation OO modeling “Has-A” relationship between classes Class B is part of class A, or class... Systems - University of Information Technology 13 Package Packages are used to group classes Four reasons for using packages To locate classes To avoid naming conflicts To distribute software conveniently To protect classes Packages may be within packages (hierarchical ) One-to-one mapping of the package name and the file system directory structure 2008 © Department of Information Systems - University... Department of Information Systems - University of Information Technology 29 Modeling One-to-Many Class A has several class B’s A class (A)’s member property (p) is defined as a collection of class (B) objects class A { int i; String s; B[] p=new B[10]; } class B {…} 2008 © Department of Information Systems - University of Information Technology 30 Inheritance OO modeling “Is-A” relationship between two... variable Something like a global variable within the class Local variable Method parameter Method level variable Block level variable A variable is effective at its declaration level and all sub-levels 2008 © Department of Information Systems - University of Information Technology 20 Variable Overriding Variables cannot be declared twice at the same level and its sub-levels except class member variables... can be used with method It is good to use “this” explicitly for member variables 2008 © Department of Information Systems - University of Information Technology 21 Outline Objects and Classes OOP Concepts Encapsulation Aggregation/Composition Inheritance & Polymorphism Some useful Java classes Exception Handling, I/O Readings & Exercise Discussion 2008 © Department of Information Systems - University . 1 Web Programming with Java Java Object-Oriented Programming Huynh Huu Viet Email: viethh@uit.edu.vn Department of Information Systems 2008 © Department of Information Systems - University. Technology 7 Object-Oriented Programming  Class/object is the focus of OOP  At design time  Class is the basic programming unit; a program consists of one or more classes  Programming focuses. Systems - University of Information Technology 15 Java Packages  Java hierarchically organizes classes into packages  java. lang  java. text  java. util  …  Classes need to be referred using

Ngày đăng: 27/06/2014, 21:20

Từ khóa liên quan

Mục lục

  • Web Programming with Java

  • Outline

  • Objects and Classes

  • Basic OO concepts : Object

  • Basic OO concepts : Class

  • Basic OO concepts : Message

  • Object-Oriented Programming

  • Constructor

  • Using Constructor

  • Constructor Overloading

  • Using Objects (1)

  • Using Objects: using methods

  • The “main” Method

  • Package

  • Java Packages

  • Using Package

  • Default Package

  • Reference Data Type

  • Object Assignment

  • Variable Scope

Tài liệu cùng người dùng

Tài liệu liên quan