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.

Slides:



Advertisements
Similar presentations
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
Advertisements

Copyright © Texas Education Agency, Computer Programming Class Basics.
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
10-Jun-15 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.
Introduction to Programming with Java, for Beginners Intro OOP with Java Java Program Structure.
Starting Classes and Methods. Objects have behaviors In old style programming, you had: –data, which was completely passive –functions, which could manipulate.
20-Jun-15 Methods. Complexity The programmer's biggest adversary is complexity The programmer's biggest adversary is complexity Primitive types and the.
Classes and Objects in Java
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
28-Jun-15 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.
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.
Defining Classes and Methods Chapter 4.1. Key Features of Objects An object has identity (it acts as a single whole). An object has state (it has various.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
MIT AITI 2003 Lecture 7 Class and Object - Part I.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Inheritance, Static and Dynamic Typing. Announcements  Project 0 due Fri 2/13 at 11:59pm 24 slip hours (unused hours roll over)  HW3 released tonight,
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Applications in Java Towson University *Ref:
Introduction to Objects A way to create our own types.
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.
The Java Programming Language
Lecture 2: Classes and Objects, using Scanner and String.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
CSC 142 Computer Science II Zhen Jiang West Chester University
Copyright © Curt Hill Turtles The beginning of media computation.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Classes and Objects in Java
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
More on Objects Mehdi Einali Advanced Programming in Java 1.
Peyman Dodangeh Sharif University of Technology Spring 2014.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
© 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.
OOP Basics Classes & Methods (c) IDMS/SQL News
BY:- TOPS Technologies
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( 李德成 )
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.
Comp1004: Introduction III Java. Content How Java Works: The JVM Writing a Class in Java – Class – Member Variables – Method – Statement Magic incantations.
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
Classes and Objects in Java
Introduction to Programming with Java, for Beginners
Classes and Objects in Java
Building Java Programs
CLASSES, AND OBJECTS A FIRST LOOK
Overview of Java 6-Apr-19.
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.
Classes and Objects in Java
Presentation transcript:

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 Here is an example class: class Dog {...description of a dog goes here... } Here are some objects of that class:

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

4 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

5 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

6 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!

7 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; } }

8 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)

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

10 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)

11 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

12 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

13 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!"); } } non-static method bark() cannot be referenced from a static context new Dog("Fido", 5).bark();

14 A complete program  class Dog { String name; int age; Dog(String n, int age) { name = n; this.age = age; } void bark() { System.out.println("Woof!"); }  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

15 The End “I invented the term ‘Object-Oriented’, and I can tell you I did not have C++ in mind.” --Alan Kay, creator of Smalltalk.