Section 11.1 Class Variables and Methods

Slides:



Advertisements
Similar presentations
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Advertisements

Chapter 10 Introduction to Arrays
Chapter 6 Introduction to Defining Classes
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 11 Classes Continued
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 10 Classes Continued
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
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 12 Support for Object oriented Programming.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 13 ATM Case Study Part 2: Implementing an Object-Oriented Design Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Chapter 5 Introduction to Defining Classes
Object-Oriented Programming Chapter Chapter
Object Oriented Programming
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
Introduction to Object-Oriented Programming Lesson 2.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Object-Oriented Design Concepts University of Sunderland.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
ISBN Chapter 12 Support for Object-Oriented Programming.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
1 Section 11.4 Java Interfaces – The Implementation Perspective Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
1 Sections 3.1 – 3.2a Basic Syntax and Semantics Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Sections 10.1 – 10.4 Introduction to Arrays
Object-Oriented Design
Web Design & Development Lecture 9
Advanced Programming in Java
Advanced Programming in Java
Sections Inheritance and Abstract Classes
9.1 Class (static) Variables and Methods
Sections Basic Concepts of Programming
Interfaces.
Inheritance and Polymorphism
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Types of Programming Languages
Object-Oriented Programming Using C++
Introduction to Object-oriented Program Design
Object-Oriented Programming
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Java Programming
Java – Inheritance.
Object-Oriented Programming
Advanced Programming in Java
Fundaments of Game Design
Object-Oriented Programming Using C++
Object-Oriented Programming
Fundaments of Game Design
Presentation transcript:

Section 11.1 Class Variables and Methods Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne

Introduction The real power of object-oriented programming is the capacity to reduce code and distribute responsibilities for such things are error handling in a software system. Static variables and methods: when information that needs to be stared among all instances of a class it is represented by static variables and accessed by static methods. 2 2

Introduction (continued) Interfaces: way of requiring a class to implement a set of methods and a way of informing clients about services. The glue that holds together cooperating classes. Inheritance: mechanism for reusing code by extending characteristics through a hierarchy. Abstract class: uninstantiated class used to define common features and behavior of a subclass. 3 3

Introduction (continued) Polymorphism: when similar methods in different classes use the same name. Preconditions: specify the use of methods. Postconditions: results if preconditions are met. Exceptions: halt the program at an error. Reference types: issues when comparing and copying objects (identity of an object; there can be multiple references to the same object). 4 4

Class (static) Variables and Methods An instance variable belongs to an object and is an allocated storage when the object is created. Each object has its own set of instance variables. A instance method is activated when a message is sent to the object. Class variables belong to a class. Storage is allocated at program startup and is independent of number of instances created. 5 5

Class (static) Variables and Methods (continued) Class method: activated when a message is sent to the class rather than the object. The static modifier designates class variables and methods. Counting the Number of Students Instantiated: Example: count student objects instantiated during execution of an application. 6 6

Class (static) Variables and Methods (continued) Counting the Number of Students Instantiated (cont): Introduce studentCount variable. Incremented each time a student object is instantiated. Because it is independent of any particular student object, it must be a class variable. Method to access studentCount variable. getStudentCount returns variable’s value on demand. Does not manipulate any particular student object, so must be a class method. 7 7

Class (static) Variables and Methods (continued) Modifying the Student Class: Add the class variable and method to class template. 8 8

Class (static) Variables and Methods (continued) Class Constants: Class constant value is assigned when a variable is declared and cannot be changed. Names are usually capitalized. Example: max in class Math returns the maximum of two parameters and min returns the minimum. Public because clients might like to access them. 9 9

Class (static) Variables and Methods (continued) Rules for Using static Variables: Class method can reference only static variables (not instance). Instance methods can reference static and instance variables. The Math Class Revisited: All of the methods and variables in the example Math class are static. 10 10