Chapter 5 Classes.

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

Computer Science and Engineering College of Engineering The Ohio State University Classes and Objects: Members, Visibility The credit for these slides.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Lecture 2: Object Oriented Programming I
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.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
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.
CS 2511 Fall Features of Object Oriented Technology  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
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.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
ECE122 Feb. 22, Any question on Vehicle sample code?
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
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 2800 Lake Sumter State College Mark Wilson, Instructor.
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.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
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.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Topics Instance variables, set and get methods Encapsulation
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Topic: Classes and Objects
OOP: Encapsulation &Abstraction
Classes (Part 1) Lecture 3
JAVA MULTIPLE CHOICE QUESTION.
Abstract Data Types and Encapsulation Concepts
Examples of Classes & Objects
Inheritance and Polymorphism
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
Object Based Programming
Chapter 9 Inheritance and Polymorphism
Inheritance 2nd Lecture
Abstract Data Types and Encapsulation Concepts
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Overloading and Overriding
Classes & Objects: Examples
Inheritance 2nd Lecture
Method of Classes Chapter 7, page 155 Lecture /4/6.
CIS 199 Final Review.
Java Programming Language
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Chapter 5 Classes

Class and member scope A top-level class (i.e., a class not nested inside another) has either package or public scope: class C { /*...*/ } public class Q { /*...*/ } Package scope is the default. A class must be declared as public to have public scope.

Class and member scope A class with package scope is accessible in the same package, whether the package be default or explicitly named. A class with public scope is accessible outside of its containing package as well as inside of its containing package.

Class and member scope Class members can have private, package, protected, or public scope. Package scope is the default for class members just as package scope is the default for classes. To have nonpackage scope, a member must be declared explicitly as private, protected, or public.

Class and member scope The class declaration public class C { private int n1; // private int n2; // package protected int n3; // protected public int n4; // public } illustrates possible member scopes.

Class and member scope A member with private scope is accessible only within its encapsulating class. A member with package scope is accessible only within its package. A member with protected scope is accessible in its containing package and in subclasses of its encapsulating class.

Class and member scope A member with public scope is accessible wherever its encapsulating class is accessible. If a member with public scope is encapsulated in a class with package scope, the public member is accessible only in the containing package.

Class and member scope Information hiding dictates that a class and its members should have the narrowest possible scope consistent with class services. Public members are typically constructors and high-level methods.

Constructors Constructors are used with the new operator to construct class instances. A concrete class is a class for which instances can be constructed. An abstract class cannot have instances. A constructor must have the name of its encapsulating class and no return type or void in place of a return type.

Constructors Constructors can have any member scope. There are two ways for a class to have a public no-argument constructor: The class defines no constructors at all. In this case, the compiler in effect provides a public no-argument constructor. The class explicitly defines a public no-argument constructor.

Constructors If a class defines any constructors at all, the compiler does not provide a public no-argument constructor. Constructors can be used to restrict and to guide object construction. For instance, if the Emp class’s only constructor expects employee’s name as a string, then only named Employees can be constructed.

Constructors Unreferenced objects can be constructed. The code segment System.out.println( new Date() ); illustrates. Constructors are typically overloaded; that is, a class typically has several constructors.

Methods Methods can have private, package, protected, or public scope. Methods must have a return type or void in place of a return type, if the method does not return a value. Methods can be overridden so long as the methods differ in name or number and order of argument types.

Methods A return type cannot be used to distinguish two methods. The code segment class C { int m() { /*...*/ } float m() { /*...*/ } //** ERROR } illustrates.

Methods Related pairs of get/set methods can be used to define class properties. The code segment class Window { void setBorder( Color c ){/*...*/} Color getBorder() {/*...*/} //... } illustrates with a color border property.

Methods A method can construct an object of its encapsulating class’s type and return a reference to the object. Such methods are known as factory methods and are an alternative to explicit constructor invocations. A static method can access only other static members.

Fields A field can have private, package, protected, or public scope. Because fields often provide low-level implementation support, they are often private. A field has a default value if not initialized in its declaration. Zero is the default value for numbers and false for booleans. Field object references default to null.

Class libraries Related classes can be collected into a package, which then serves as a software library. The standard packages provide many rich examples. For instance, the java.net package provides a class library for network programming.

Class libraries A good way to gain fluency in a class or class library is to write a test client, that is, a program that explores and tests a class’s or a class library’s services. A programmer-defined class should be accompanied by a test client that illustrates typical client use of the class.