CLASSES, AND OBJECTS A FIRST LOOK

Slides:



Advertisements
Similar presentations
Copyright © Texas Education Agency, Computer Programming Class Basics.
Advertisements

CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.
Introduction to Programming with Java, for Beginners Intro OOP with Java Java Program Structure.
Classes and Objects in Java
29-Jun-15 Using Objects. 2 Classes and objects The type of an object is the class that describes that object If we say int count, the type of count is.
10-Aug-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is an abstract description of objects.
MIT AITI 2003 Lecture 7 Class and Object - Part I.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Object Oriented Software Development
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
Static Keyword. What is static The static keyword is used when a member variable of a class has to be shared between all the instances of the class. All.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Classes and Objects in Java
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.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
3-July-2002cse142-D2-Methods © 2002 University of Washington1 Methods CSE 142, Summer 2002 Computer Programming 1
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Comp1004: Introduction III Java. Content How Java Works: The JVM Writing a Class in Java – Class – Member Variables – Method – Statement Magic incantations.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Content Programming Overview The JVM A brief look at Structure – Class – Method – Statement Magic incantations – main() – output Coding a Dog Programming.
Chapter 4 - Finishing the Crab Game
Object-Oriented programming for Beginners LEAPS Computing 2015
Summary prepared by Kirk Scott
Concepts of Object Oriented Programming
Building Java Programs
Content Programming Overview The JVM A brief look at Structure
Static data members Constructors and Destructors
Objects as a programming concept
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Classes and Objects in Java
C++ Classes & Object Oriented Programming
Creating and Modifying Text part 2
Class Variables We’ve seen how to add extra subroutines into our programs Can be accessed from within one another We can also add variables that are “global”
Advanced Programming in Java
Exceptions 10-Nov-18.
Advanced Programming in Java
Introduction to Programming with Java, for Beginners
Class Structure 16-Nov-18.
Defining Classes and Methods
Variables ICS2O.
Class Structure 28-Nov-18.
Fundamentals of Programming
Class Structure 7-Dec-18.
Building Java Programs
CS18000: Problem Solving and Object-Oriented Programming
Class Structure 2-Jan-19.
Classes and Objects in Java
METHODS, CLASSES, AND OBJECTS A FIRST LOOK
Class Structure 25-Feb-19.
Building Java Programs
Overview of Java 6-Apr-19.
Building Java Programs
Classes, Objects and Methods
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Lecture 8-2: Object Behavior (Methods) and Constructors
Methods 16-May-19.
Classes and Objects in Java
CMSC 202 Exceptions.
Basic Exception Handling
Classes and Methods 15-Aug-19.
The beginning of media computation Followed by a demo
Presentation transcript:

CLASSES, AND OBJECTS A FIRST LOOK Prof. Salim Arfaoui

OBJECT ORIENTED PROGRAMMING Next What Are Classes and Objects? DEFINING CLASSES AND CREATING OBJECTS Specifying a class: UML Creating Objects Displaying an Object ADDING METHODS TO CLASSES

Classes and Objects A Java program consists of one or more classes A class is an abstract description of objects Here is an example class: class Dog { ...description of a dog goes here... } Here are some objects of that class:

More Objects Here is another example of a class: class Window { ... } Here are some examples of Windows:

Classes contain data definitions Classes describe the data held by each of its objects Example: class Dog { String name; int age; ...rest of the class... } Data usually goes first in a class A class may describe any number of objects Examples: "Fido", 3; "Rover", 5; "Spot", 3; A class may describe a single object, or even no objects at all

Classes contain methods A class may contain methods that describe the behavior of objects Example: class Dog { ... void bark() { System.out.println("Woof!"); } } Methods usually go after the data When we ask a particular Dog to bark, it says “Woof!” Only Dog objects can bark; the class Dog cannot bark

Methods contain statements A statement causes the object to do something (A better word would be “command”—but it isn’t) Example: System.out.println("Woof!"); This causes the particular Dog to “print” (actually, display on the screen) the characters Woof!

Methods may contain temporary data Data described in a class exists in all objects of that class Example: Every Dog has its own name and age A method may contain local temporary data that exists only until the method finishes Example: void wakeTheNeighbors( ) { int i = 50; // i is a temporary variable while (i > 0) { bark( ); i = i – 1; } }

Classes always contain constructors A constructor is a piece of code that “constructs,” or creates, a new object of that class If you don’t write a constructor, Java defines one for you (behind the scenes) You can write your own constructors Example: class Dog { String name; int age; Dog(String n, int age) { name = n; this.age = age; } } (This part is the constructor)

Diagram of program structure File Class Variables Constructors A program consists of one or more classes Typically, each class is in a separate .java file Methods

Summary A program consists of one or more classes A class is a description of a kind of object In most cases, it is the objects that do the actual work A class describes data, constructors, and methods An object’s data is information about that object An object’s methods describe how the object behaves A constructor is used to create objects of the class Methods (and constructors) may contain temporary data and statements (commands)

Writing and running programs When you write a program, you are writing classes and all the things that go into classes Your program typically contains commands to create objects (that is, “calls” to constructors) Analogy: A class is like a cookie cutter, objects are like cookies. When you run a program, it creates objects, and those objects interact with one another and do whatever they do to cause something to happen Analogy: Writing a program is like writing the rules to a game; running a program is like actually playing the game You never know how well the rules are going to work until you try them out

Getting started Question: Where do objects come from? Answer: They are created by other objects. Question: Where does the first object come from? Answer: Programs have a special main method, not part of any object, that is executed in order to get things started public static void main(String[ ] args) { Dog fido = new Dog("Fido", 5); // creates a Dog } The special keyword static says that the main method belongs to the class itself, not to objects of the class Hence, the main method can be “called” before any objects are created Usually, the main method gets things started by creating one or more objects and telling them what to do

A bad program public class Dog { String name; int age; Dog(String name, int age) { this.name = name; this.age = age; } public static void main(String[] args) { bark(); } void bark() { System.out.println("Woof!"); } } --------- new Dog("Fido", 5).bark(); non-static method bark() cannot be referenced from a static context

A complete program void wakeTheNeighbors( ) { int i = 50; while (i > 0) { bark( ); i = i – 1; } } public static void main(String[ ] args) { Dog fido = new Dog("Fido", 5); fido.wakeTheNeighbors(); } } // ends the class class Dog { String name; int age; Dog(String n, int age) { name = n; this.age = age; } void bark() { System.out.println("Woof!"); }