AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Fields, Constructors, Methods
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Composition details Recall, composition involves 3 things: –Declaration of instance variable of.
Encapsulation CMSC 202. Types of Programmers Class programmers – Developers of new classes – Goal: Expose the minimum interface necessary to use a new.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Chapter 4 Objects and Classes.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 (part 2) Khalid Siddiqui.
Two Parts of Every ADT An abstract data type (ADT)  is a type for encapsulating related data  is abstract in the sense that it hides distracting implementation.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
AP Computer Science A – Healdsburg High School 1 Unit 3 - Objects and Classes Lab: First Steps - Example.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
ACO 101: Intro to Computer Science Anatomy Part 3: The Constructor.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
AP Computer Science A – Healdsburg High School 1 static Keyword in Java - Static variables - Static methods.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
CLASS AND OBJECTS Valerie Chu, Ph.D. LeMoyne-Owen College 1.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Constructors A constructor is a method that has the same name as the class itself It is automatically called when an object is instantiated (in other words,
Topics Instance variables, set and get methods Encapsulation
Dale Roberts Object Oriented Programming using Java - Getters and Setters Dale Roberts, Lecturer Computer Science, IUPUI
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
CS16: UML’s Unified Modeling Language. UML Class Diagram A UML class diagram is a graphical tool that can aid in the design of a class. The diagram has.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
OOP: Encapsulation &Abstraction
Objects as a programming concept
Classes (Part 1) Lecture 3
Chapter 3: Using Methods, Classes, and Objects
More about OOP and ADTs Classes
Chapter 4: Writing Classes
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Classes and Objects: Encapsulation
Classes and Objects Encapsulation
Learning Objectives Classes Constructors Principles of OOP
© A+ Computer Science - OOP © A+ Computer Science -
Defining Classes and Methods
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Which best describes the relationship between classes and objects?
CS360 Client/Server Programming Using Java
JAVA CLASSES.
CMSC 202 Encapsulation Version 9/10.
Classes and Objects Systems Programming.
CSG2H3 Object Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class

AP Computer Science A – Healdsburg High School 2 Encapsulation A class (in OOP) encapsulates (groups together) data and actions relating to an object. Encapsulation hides the implementation details of the class from the users (clients) of the class.

AP Computer Science A – Healdsburg High School 3 Encapsulation (cont’d) public class MyClass { // Private fields: private myField;... // Constructors: public MyClass (...) {... }... // Public methods: public myMethod (...) {... }... // Private methods: private myMethod (...) {... }... } Public interface: public constructors and methods

AP Computer Science A – Healdsburg High School 4 1. Fields (or instance variables) Fields describe the data of an object (think nouns!!). Fields are generally labeled private and therefore hidden from the client (only accessible from within the class).

AP Computer Science A – Healdsburg High School 5 2. Constructors Constructors describe ways to create an object of a class and initialize the fields. Constructors are always labeled public and have the same name as the class. There may be more than one constructor for a class. If more than one, each must have different arguments (or parameters).

AP Computer Science A – Healdsburg High School 6 3. Public Methods Methods describe the actions or questions that can be asked about an object (think verbs!!). Methods are generally labeled public. private (helper) methods can only be used within the class.

AP Computer Science A – Healdsburg High School 7 3. Public Methods (cont.) In general, 3 classifications of methods: 1. Accessor – “gets” information about the object. 2. Modifier – “sets” information about the object (or changes the state of the object). 3. boolean – asks a true or false question about the object.

AP Computer Science A – Healdsburg High School 8 3. Public Methods (cont.) To define a method: –decide between public and private (usually public) –give it a name –specify the types of parameters and give them names –specify the method’s return type or choose void –write the method’s code public [or private] returnType methodName (type1 name1,..., typeN nameN) {... } Header Body