Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,

Slides:



Advertisements
Similar presentations
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
Advertisements

Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012.
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.
Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
CSCI 160 Midterm Review Rasanjalee DM.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
 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.
Chapter 6 Introduction to Defining Classes
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
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.
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)
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Objects and Classes. 3-2 Object Oriented Programming  An OO program models the application as a world of interacting objects.  A typical Java program.
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Introduction to Programming Writing Java Beginning Java Programs.
Java Classes Using Java Classes Introduction to UML.
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.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 CSC241: Object Oriented Programming Lecture No 02.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Object Oriented Programming Object and Classes Lecture 3 MBY.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Defining Classes. Why is Java designed this way? Improving our class Creating our own class Using our class Implementing our class.
Features of JAVA PLATFORM INDEPENDENT LANGUAGE JAVA RUNTIME ENVIRONMENT (JRE) JAVA VIRTUAL MACHINE (JVM) JAVA APP BYTE CODE JAVA RUNTIME ENVIRONMENT.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
A Simple Object Oriented Program public class Simple { public static void main (String [] args) { System.out.println(“howdy”); } System.out is an object.
3 Introduction to Classes and Objects.
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
CompSci 230 Software Construction
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Session 2: Introduction to Object Oriented Programming
Object Oriented Programming in java
Introduction to Object-Oriented Programming
In this class, we will cover:
Presentation transcript:

Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions, a Java program can implement a GUI or send and receive information over a network  An object incorporates some data (variables) and actions (methods) associated with that data.  Classes are blue prints for objects  Classes specify the data (variables) and actions (methods) possessed both by themselves and by objects built from them

Classes  Objects fall into different categories or classes e.g. you are a student and belong to the class student and so is the person next to you; both of you are students  Objects created from the same class have the same range of behaviours (methods) and the same attributes (age, grades, subjects (variables) ) but probably with some different values

Classes  The class body contains all of the code that provides for the lifecycle of the objects created from it –Constructors for initialising new objects –Declarations for the variables that provide the state of objects –Methods to implement the behaviour of the class and its objects  Every class should be saved in its own.java file and saved with the name of the class. All classes in the one program should be saved in the same directory

Object Instantiation  There are TWO parts to instantiating an object from a class  The first procedure is a variable of the desired object has to be declared in the program –Student someStudent; (object variableName;)  This notifies the compiler that you will use someStudent to refer to data whose type is student  Declarations do not create new objects. The code Student someStudent does not create a new Student object; it just declares a variable, named someStudent that will be used to refer to a Student object.

Object Instantiation  The Second set up procedure sets aside memory for all the information about this student object  The new operator instantiates a class by allocating memory for a new object. The new operator requires a single argument after the new keyword: a call to a constructor. –someStudent = new Student ();  The new operator returns a reference to the object it created. The someStudent variable is called a reference to an instance of the student object  It is common to combine the two statement as follows: –Student someStudent = new Student();

Constructors  A constructor is a method that creates an object. In java constructors are instance methods with the same name as their class. Constructors are invoked using the new keyword  A constructor is a special method which is executed only when an object is created  The purpose of a constructor is to setup (or construct) the environment for the object (variable values, etc) and to initialise an objects data when it is created  A constructor method is easily recognised as it must: –Have the same name as the class

Instance Variables car car1 = new car(); car1 model: null speed:0 currentGear:0

Instance Variables  Instance Variables are declared like local variables public class car{ String model; int speed; int currentGear: } ……but may be initialised to default values

Defining Constructor – Car Example Public class car { Private String Model: … Public Car () …{ Public Car (String make) { model = make; … }} Car car1 = new Car(); Car car 2 = new car ("Audi");

Constructors: Summary  A constructor is generally the first method in a class - it has the exact same name as the class (but has parentheses and optional arguments)  The arguments in a constructor are typically used to initialise the instance variables of the class.  Java has a default constructor if none is provided which does not initialisation - this constructor is called the no args constructor.