Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class.

Slides:



Advertisements
Similar presentations
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented.
1 Java Programming Basics SE-1011 Dr. Mark L. Hornick.
IT151: Introduction to Programming
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Road Map Introduction to object oriented programming. Classes
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
UML Basics & Access Modifier
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
4.1 Instance Variables, Constructors, and Methods.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
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 10 Introduction to Classes
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Documentation and Style. Documentation and Comments  Programs should be self-documenting.  Use meaningful variable names.  Use indentation and white.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
OOP Basics Classes & Methods (c) IDMS/SQL News
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Objects as a programming concept
Classes and OOP.
Chapter 3: Using Methods, Classes, and Objects
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Chapter 5 Classes.
Can perform actions and provide communication
Chapter 3 Introduction to Classes, Objects Methods and Strings
Can perform actions and provide communication
Defining Classes and Methods
Object Oriented Programming
Can perform actions and provide communication
Workshop for Programming And Systems Management Teachers
Documentation and Style
Anatomy of a Java Program
Object Oriented Programming in java
Defining Classes and Methods
Creating and Using Classes
Presentation transcript:

Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class.

Classes (cont’d) The understanding of what classes and objects are can be a little confusing at first. Many texts uses a number of different examples and analogies to try and explain the concept of a class. Lets look at a few. In doing so you can pick the one that makes the most sense to you.

The Recipe Analogy A class might be thought of as a recipe for making a cake. Within the recipe would be listed all the ingredients for making the cake (variables) We’d also find instructions about what actions would be necessary to turn the ingredients into a completed cake. (methods)

public class cake { milk sugar flour bake(){ mix milk, sugar and flour in bowl, heat …… } Class name variables method

Class vs Object Having a class does not mean any objects have actually been created. Like the recipe, a class is only a description of the components and the actions associated with a potential object.

Class vs Object Cake, as a concept,, is something that we know and understand but it is not something that we can see, touch or eat. A cake, in contrast, is something that we can see and eat. Compare the cake that mom just made sitting in the fridge (topped with chocolate icing that came with a warning of dire consequences if we touch it before the party) with the recipe for that cake. This distinction is crucial in understanding how classes are used in programming.

A class defines a new type A class is a description of an object An object is an instance of a class. We say that we instantiate an object when we create it. Every object is Java must have a class associated with it. The class is the type (and these two terms are often used interchangeably).

Instantiating an Object To declare an object entitled MyCake of class cake use the following syntax: cake MyCake = new cake(); The class cake describes all the qualities of the object MyCake. MyCake is an object of class(or type) cake.

Instantiating an Object You can declare more cake objects: cake MyCake2 = new cake(); cake yourCake = new cake(); cake bDayCake = new cake();

The Factory Analogy Consider a factory whose purpose is to make chairs The factory itself is not a chair. It is a mechanism for making chairs. The factory is designed to be the most efficient and functional chair maker it can be.

The Factory Analogy We can design and build a factory but the factory can sit idle until it receives an order to construct some chairs. An class is like a factory. It’s job is to construct a certain type of object when it receives an order (keyword: new )

new The keyword new tells the java compiler that we are trying to declare an object. Compare: int myNum; and cake myCake = new cake(); In the 1 st case we’re declaring a primitive data type. In the 2 nd case we’re declaring an abstract one.

Circle class Look at the Circle class on p181 of your textbook. We have some variables We have something called a constructor. (Don’t worry for the moment) We have a method to set the radius setRadius()

Circle class We have a method to calculate the area of a circle – area() We have a method to return the radius of a circle – getRadius()

Let’s try some code Start a new JCreator project and call it Circle. Enter the code from p181 of your text. Do not delete the main method. Build the code. Modify the main method to create a new circle. Set the radius to 5. Call the area() method to print the area Call the getRadius() method to print the radius.

More About Objects Objects have two generic qualities: –state: reflects the data stored by the object think variables (e.g. radius ) –behaviour: the actions it can take and the communication it provides think methods (e.g. area() )

Good Programming Style for Objects An object’s state should only be changed through it’s behaviour. If we want to change the radius of a circle object we only allow this to be done via the setRadius() method. Protecting an object’s data in this way is called encapsulation.

Client Code Our text uses the paradigm of client code Client Code is an application that uses one or more classes. It can access the class’ methods but not it’s data. This reinforces… …encapsulation

Chapter 8 A Class public class Circle { private static final double PI = 3.14; private double radius; public Circle() { radius = 1; } public void setRadius(double newRadius) { radius = newRadius; } access level class name body variables constructor method

Class Design Each class should be written in it’s own file. The name of the file should be the same as the class name (including capitalization) with the.java file extension appended.

Class Design Just like methods, classes have declarations The class declaration consists of: – an access level ( public or private ) –the keyword class –the class name Everything inside the braces is the class body

Class Design (cont’d) The class body is typically composed of: –variables –one or more constructors a piece of code executed when an object is created from the class (more to come, later) –methods

Class Design -conventions Class names should be nouns Class names should begin with uppercase letters If the class name is composed of more than one word, each subsequent word should be uppercase Like other java identifiers, spaces are not allowed.

Class Design (cont’d) An opening brace (“{“) begins a class A closing brace (“}”) ends a class Class variables are declared at the top of the class after the opening brace Class variables are declared outside of the class methods

Class Design (cont’d) Class variables should have an access level of private This makes them visible to the class but not to client code which can then only access them via method calls. What’s that called again? Riiiggghhht. Encapsulation.

Class Design (cont’d) Class methods fall into 3 categories: –accessor methods called to determine the value of a variable (e.g. getRadius() ) –modifier methods called to change the value of a variable (e.g. setRadius() ) –helper methods called by other methods in the class to perform tasks (no e.g. in the Circle class)

You do Pg 182 Review: Circle - part 1 of 4 Pg 182 Review: Coin - part 1 of 2 –Coin involves random numbers. See pg or the web