Classes and Objects in Java

Slides:



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

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.
© Vinny Cahill 1 Classes in Java. © Vinny Cahill 2 Writing a Java class Recall the program to calculate the area and perimeter of a rectangle of given.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Classes and Objects in Java
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.
Computer Science A 1: 3/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Methods: a first introduction Two ways to make tea: 1: boil water; put teabag into cup;... etc : tell your younger brother: makeTea(1 milk, 0 sugar);
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.
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:
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,
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
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.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Java Programming. History F James Gosling and Sun Microsystems F Oak F Java, May 20, 1995, Sun World F HotJava –The first Java-enabled.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
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.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Procedural programming in Java Methods, parameters and return values.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
CSC 142 Computer Science II Zhen Jiang West Chester University
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
Methods OR HOW TO MAKE A BIG PROGRAM SEEM SMALLER.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Computer Programming1 Computer Science 1 Computer Programming.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Defining Classes. Why is Java designed this way? Improving our class Creating our own class Using our class Implementing our class.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
Problem of the Day  Why are manhole covers round?
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.
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 – Class – Method – Statement Magic incantations – main() – output Coding a Dog Programming.
Content Programming Overview The JVM A brief look at Structure
Classes and Objects in Java
Introduction to Programming with Java, for Beginners
Introduction to Computer Programming
Classes and Objects in Java
Take out a piece of paper and PEN.
class PrintOnetoTen { public static void main(String args[]) {
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
Object-Oriented Programming and class Design
Presentation transcript:

Classes and Objects in Java 24-Apr-17

Classes and Objects A Java program consists of one or more classes A class is the description; while objects are actual examples 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 (objects) of Windows:

Classes are like blueprints While the house made from the blueprint is the object

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

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) The constructor always has the same name as your class. Example: class Dog { String name; int age; Dog(String n, int age) { name = n; this.age = age; } } (This part is the constructor)

Constructors There are 3 constructors: You can have multiple constructors Example: class Dog { String name; int age; Dog(String n, int age) { name = n; this.age = age; } Dog(int age) { name = “UNKNOWN”; this.age = age; } Dog() { name = “UNKNOWN”; age = 0; } } There are 3 constructors: It will construct an object based on the # of arguments.

Diagram of program structure File Class Variables Constructors Variables Statements 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)

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 }

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!"); } } public class Kennel { public static void main(String[ ] args) { Dog fido = new Dog("Fido", 5); fido.bark(); } } // ends the class