Dr. R Z Khan Handout-3 Classes

Slides:



Advertisements
Similar presentations
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.
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
1 Basic Object Oriented Concepts Overview l What is Object-Orientation about? l What is an Object? l What is a Class? l Constructing Objects from Classes.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
1 More on Classes Overview l Overloading l The this keyword l The toString method l The equals method.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Writing Classes (Chapter 4)
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
1 Object state: fields. Fields field: A variable inside an object that represents part of its internal state.  Each object will have its own copy of.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
CSC 142 Computer Science II Zhen Jiang West Chester University
Chapter 4 Introduction to Classes, Objects, Methods and strings
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
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.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
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.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Object Oriented Programming
Building Java Programs
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
CSC240 Computer Science III
Classes and Objects, State and Behavior
Chapter 4: Writing classes
Object initialization: constructors
Building Java Programs
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Group Status Project Status.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Outline Anatomy of a Class Encapsulation Anatomy of a Method
JAVA CLASSES.
Handout-4b More on Classes
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Introduction to Object-Oriented Programming
Defining Classes and Methods
Building Java Programs
Classes and objects Readings: 8.1.
Building Java Programs
Building Java Programs
Handout-2(a) Basic Object Oriented Concepts
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
Building Java Programs
Chapter 11 Inheritance and Encapsulation and Polymorphism
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Dr. R Z Khan Handout-3 Classes Overview Classes as Types Declaring Instance Variables Implementing Methods Constructors Accessor and Mutator Methods

Classes as Types So far we have learnt that Java is an object-oriented programming language, which means problems are solved by interactions among objects. That objects are created from classes, so our main task as Java programmers is designing classes that can be used as blueprint for creating objects. We have used many standard classes to create objects and used then in our applications and applets – e.g. Rectangle, Color, String, etc. We have also implemented a number of classes and applets. However, those classes were not designed to represent any object. That is, they were not designed to serve as blueprint for creating objects. To be real Java (or object-oriented) programmers, we must learn how to design classes that can be used to create objects.

Classes as Types (Cont’d) Such classes behaves as types as the following table shows: To design a class for use as type, we need to identify the following: Attributes or fields that stores the state of members (objects) of the class Methods describing the behavior of members of the class. For example, suppose we wish to design a class for creating point objects on a plane Then the following might be the attributes: x-coordinate y-corordinate And the following might be behaviors: computing distance to origin Computing distance to another point Moving the point to another position. int n1, n2; n1=10; n2 = 12; Point p1, p2; p1 = new Point(5, 10); p2 = new Point(15, 6);

Declaring Instance Variables Fields of an object are represented in its class as instance variables. An instance variable have the following parts: An access modifier (usually private) The type of the variable (e.g. int) The name of the variable (e.g. x) Instance variables are declared inside the class body but not inside any method. public class Point { private int x; private int y; .... } Notice that instance variables must NOT be declared as static. Static variables cannot have multiple copies in the memory. They have only one copy that belongs to the class, thus they are often called class variables. ‘

Implementing Methods The behaviors of a class are implemented as non-static methods. The method header of such a method consists of : An access modifier (usually public) A return type (e.g. double). If the method does not return any values, void is used. The name of the method Zero or more parameters enclosed in parenthesis Example: public class Point { private int x; private int y; public double distanceToOrigin() { return Math.sqrt(x*x + y*y); } . . . .

Constructors In addition to instance variables and methods, a class may have one or more constructors. A constructor is used at the time of constructing an object of the class to initialize the instance variables of the object. E,g, Point p = new Point(10, 20); A constructor is similar to a method except for the following differences. A constructor does not have a return type A constructor must have the same name as the class A constructor must be declared as public. Example: public class Point { private int x; private int y; public Point (int x1, int y1) { x = x1; y = y1; } public double distanceToOrigin() { return Math.sqrt(x*x + y*y); . . . .

Constructors (cont’d) If a constructor is not defined for a class, the compiler automatically inserts a code for the default constructor. A default constructor is the one without any parameters. It usually initializes the instant variables with their default values. The following table shows the default values for different types. If a constructor is defined for a class, the default constructor is not provided by the compiler.

Mutator and Accessor Methods Object-oriented languages exhibit what is called encapsulation. This means putting the data and the methods that operates on it as a single unit – object The principle of encapsulation dictates that an object should have total control of its data. That is, another object should not change the data without the owner knowing. This is achieved in java by declaring instance variables as private. However, an object may provide access to the values (copy) of its variables by providing a public method that returns such values. Such methods are called accessor (or get) methods. Sometimes an object cannot help but provide methods that changes the values of its variables. Such methods are called Mutator methods. The next example shows a complete implementation of the Point class.

The Point Class Example public class Point { private double x; private double y; public Point(double x1, double y1) { x=x1; y=y1; } public double distanceToOrigin() { return Math.sqrt((x * x) + (y * y)); public double distanceToPoint(Point p) { return Math.sqrt(((x - p.x)*(x - p.x))+((y - p.y)*(y - p.y))); public void moveTo(double x1, double y1) { public void translate(double dx, double dy) { x += dx; y += dy; public double getX() { return x; public double getY() { return y; }}

The Point Class Example (cont’d) The following example shows how the Point class may be used. public class TestPoint { public static void main(String[] args) { Point p1 = new Point(10, 20); Point p2 = new Point(15, 12); System.out.println("Distance of "+p1+ " to origin = "+p1.distanceToOrigin()); System.out.println("Distance of "+p2+ " to origin = "+p2.distanceToOrigin()); System.out.println("Distance between "+p1+" and "+p2+ " = "+p1.distanceToPoint(p2)); } Notice that we did not use all the methods of the Point class in the above example. When designing a class, we should not pay attention to a specific application that may use the class, rather, we should provide methods that implements all the behaviors of the class that we can think of This makes the class to be useful in different applications.