1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.

Slides:



Advertisements
Similar presentations
UNIT II DATA TYPES OPERATORS AND CONTROL STATEMENT 1.
Advertisements

1 Classes and Objects in Java Basics of Classes in Java.
1 Inheritance Classes and Subclasses Or Extending a Class.
8 Copyright © 2005, Oracle. All rights reserved. Object Life Cycle and Inner Classes.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012.
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Road Map Introduction to object oriented programming. Classes
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Classes and Instances. Introduction Classes, Objects, Methods and Instance Variables Declaring a Class with a Method and Instantiating an Object of a.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Hello, world! Dissect HelloWorld.java Compile it Run it.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Shorthand operators.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
UML Basics & Access Modifier
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
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.
CSC Programming I Lecture 8 September 9, 2002.
The Java Programming Language
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
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:
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
A First Simple Program /* This is a simple Java program. Call this file "Example.java".*/ class Example { // Your program begins with a call to main().
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Classes, Interfaces and Packages
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.
OOP Basics Classes & Methods (c) IDMS/SQL News
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
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( 李德成 )
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Object Oriented Programming Introduction to Classes and Objects.
Classes and Objects.
3 Introduction to Classes and Objects.
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Interfaces.
Session 2: Introduction to Object Oriented Programming
Object Oriented Programming in java
Dr. R Z Khan Handout-3 Classes
(C) 2010 Pearson Education, Inc. All rights reserved.
Presentation transcript:

1

In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods. Instance methods belong to a particular object. Static methods belong to a particular class. 2

E XAMPLE : T HE C AR C LASS Suppose you need to write a traffic simulation program that watches cars going past an intersection. Each car has: 1. a speed. 2. a maximum speed. 3. a license plate. that uniquely identifies it. In traditional programming languages you'd have two floating point and one string variable for each car. With a class you combine these into one thing like this. 3

class Car { String licensePlate; // e.g. Palestine 543 A23" double speed; // in kilometers per hour double maxSpeed; // in kilometers per hour } These variables (licensePlate, speed and maxSpeed) are called the member variables, instance variables, or fields of the class. Fields tell you what a class is and what its properties are. 4 E XAMPLE 1: T HE C AR C LASS

An object is a specific instance of a class with particular values (possibly mutable) for the fields. While a class is a general blueprint for objects, an instance is a particular object. 5 E XAMPLE 1: T HE C AR C LASS

C ONSTRUCTING OBJECTS WITH NEW class Car { String licensePlate; // e.g. Palestine 543 A23" double speed; // in kilometers per hour double maxSpeed; // in kilometers per hour } To instantiate an object in Java, use the keyword new followed by a call to the class's constructor. Here's how you'd create a new Car variable called c: Car c; c = new Car(); 6

The first word, Car, declares the type of the variable c. Classes are types and variables of a class type need to be declared just like variables that are ints or doubles. The equals sign is the assignment operator and new is the construction operator. Finally notice the Car() method. The parentheses tell you this is a method and not a data type like the Car on the left hand side of the assignment. This is a constructor, a method that creates a new instance of a class. You'll learn more about constructors shortly. However if you do nothing, then the compiler inserts a default constructor that takes no arguments. 7 C ONSTRUCTING OBJECTS WITH NEW

This is often condensed into one line like this: Car c = new Car(); 8 C ONSTRUCTING OBJECTS WITH NEW

T HE M EMBER A CCESS S EPARATOR class Car { String licensePlate; // e.g. Palestine 543 A23" double speed; // in kilometers per hour double maxSpeed; // in kilometers per hour } Once you've constructed a car, you want to do something with it. To access the fields of the car you use the. separator. The Car class has three fields 1. licensePlate 2. speed 3. maxSpeed 9

Therefore if c is a Car object, c has three fields as well: c.licensePlate c.speed c.maxSpeed You use these just like you'd use any other variables of the same type. For instance: Car c = new Car(); c.licensePlate = Palestine 12546"; c.speed = 70.0; c.maxSpeed = ; System.out.println(c.licensePlate + " is moving at " + c.speed + "kilometers per hour.") ; The separator selects a specific member of a Car object by name. 10 T HE M EMBER A CCESS S EPARATOR

U SING A C AR OBJECT IN A DIFFERENT CLASS class Car { String licensePlate; // e.g. Palestine 543 A23" double speed; // in kilometers per hour double maxSpeed; // in kilometers per hour } The next program creates a new car, sets its fields, and prints the result: class CarTest { public static void main(String args[]) { Car c = new Car(); c.licensePlate = " Palestine "; c.speed = 70.0; c.maxSpeed = ; System.out.println(c.licensePlate + " is moving at " + c.speed + " kilometers per hour."); } 11

This program requires not just the CarTest class but also the Car class. To make them work together put the Car class in a file called Car.java. Put the CarTest class in a file called CarTest.java. Put both these files in the same directory. Then compile both files in the usual way. Finally run CarTest. For example, % javac Car.java % javac CarTest.java % java CarTest Palestine is moving at 70.0 kilometers per hour. Note that Car does not have a main() method so you cannot run it. It can exist only when called by other programs that do have main() methods. 12 U SING A C AR OBJECT IN A DIFFERENT CLASS

I NITIALIZING F IELDS Fields can (and often should) be initialized when they're declared, just like local variables. class Car { String licensePlate = "; // e.g. Palestine 543 A23" double speed = 0.0; // in kilometers per hour double maxSpeed = ; // in kilometers per hour } The next program creates a new car and prints it: class CarTest2 { public static void main(String[] args) { Car c = new Car(); System.out.println(c.licensePlate + " is moving at " + c.speed + "kilometers per hour."); } 13

I NITIALIZING F IELDS For example, $ javac Car.java $ javac CarTest2.java $ java CarTest is moving at 0.0 kilometers per hour. 14