1 / 10

Dr. R Z Khan Handout-3 Classes

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

hixon
Download Presentation

Dr. R Z Khan Handout-3 Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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.

  3. 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.

  4. 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. ‘

  5. 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); } . . . . }

  6. 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); } . . . . }

  7. 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.

  8. 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 publicmethod 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.

  9. 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) { x=x1; y=y1; } public void translate(double dx, double dy) { x += dx; y += dy; } public double getX() { return x; } public double getY() { return y; }}

  10. 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.

More Related