Classes and Objects in Java

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.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
10-Jun-15 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Introduction to Programming with Java, for Beginners Intro OOP with Java Java Program Structure.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
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.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
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,
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.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
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:
Shorthand operators.
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,
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Object Oriented Software Development
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
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
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
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.
Chapter 2: Java Fundamentals
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Introduction to programming in the Java programming language.
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
CSC 142 Computer Science II Zhen Jiang West Chester University
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.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Type soundness In a more formal way. Proving Soundness of Type Systems Goal of a sound type system: –if the program type checks, then it never “crashes”
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
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
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.
Content Programming Overview The JVM A brief look at Structure
Objects as a programming concept
Classes and Objects in Java
Introduction to Programming with Java, for Beginners
Classes and Objects in Java
CLASSES, AND OBJECTS A FIRST LOOK
Overview of Java 6-Apr-19.
CSC 111 Exam 3 Review Fall 2005.
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:

Classes and Objects in Java 17-Apr-17

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 Variables Methods A program consists of one or more classes Typically, each class is in a separate .java file

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 The main method is defined in a class, but it belongs to the class itself, not to any specific objects of that class The special keyword static says that the method belongs to the class, not to objects of the class public static void main(String[ ] args) { Dog fido = new Dog("Fido", 5); // creates a Dog }

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

The End