1 More on Classes Overview l Overloading l The this keyword l The toString method l The equals method.

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

23-May-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
CS180 Chapter 4 2/1/08. Announcements Project 3 is out –2 weeks due to the exam Exam –Thursday, February 7, 8:30 – 9:30 pm, WTHR 104 –Covers chapters.
Understanding class definitions Looking inside classes.
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.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
1 Software Construction Lab 4 Classes and Objects in Java Basics of Classes in Java.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
CSC 142 Computer Science II Zhen Jiang West Chester University
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
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.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Classes and Objects Introduced
OOP Powerpoint slides from A+ Computer Science
Lecture 11: More on Classes
Agenda Warmup AP Exam Review: Litvin A2
CSC240 Computer Science III
Overloading and Constructors
Lecture 8-3: Encapsulation, this
Building Java Programs
Topic 28 classes and objects, part 2
Building Java Programs
Building Java Programs
Building Java Programs
© A+ Computer Science - OOP © A+ Computer Science -
Classes and objects Readings: 8.1.
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Building Java Programs
Building Java Programs
Handout-4b More on Classes
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Special instance methods: toString
Building Java Programs
Classes and objects Readings: 8.1.
Building Java Programs
Dr. R Z Khan Handout-3 Classes
Building Java Programs
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
Chapter 11 Inheritance and Encapsulation and Polymorphism
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Topic 29 classes and objects, part 3
Building Java Programs
Day 11 The Last Week!.
Presentation transcript:

1 More on Classes Overview l Overloading l The this keyword l The toString method l The equals method

2 Overloading l Java allows a class to have more than one constructor. l Of course such constructors must have the same name – name of the class. This is called overloading. l For example, we could define another constructor for the Point class as follows: public class Point { private int x; private int y; public Point (int x1, int y1) { x = x1; y = y1; } public Point() ( x=0; y=0; }.... }

3 Overloading (cont’d) l Having multiple constructors gives the user a choice when constructing an object of the class. Point p1 = new Point(10, 20); Point p2 = new Point(); l Methods can also be overloaded. For example, all the following methods can be defined in one class. public double max(double a, double b) { if (a >= b) return a; else return b; } public String max(String a, String b) { if (a.compareTo(b) > 0) return a; else return b; } public Point max(Point a, Point b) { if (a.distanceToOrigin() > b.distanceToOrigin()) return a; else return b; }

4 Overloading (cont’d) l An important point to note is that overloaded methods and constructors must have different signatures. l That is, the formal parameters of overloaded constructors and methods must not have the same type, order, and count all at the same time. l The return type of a method is not counted as part of its signature. l The following shows an example of illegal overloading. public double compute(int num) {... } public int compute(int num) {... }

5 The this keyword l The this keyword is used as a reference to the current object. It has dual usage: l It can be used with a dot to refer to the fields and methods of this object. l Example. this.x means the x variable of the current object. l This method is often used to resolve scope conflict between the parameters of a method or constructor and an instance variable with the same name. l The this keyword can also be used to call another constructor of a class from another constructor of the same class. l The following shows both uses of the this keyword. class Point { private double x; private double y; public Point(double x, double y) { this.x=x; this.y=y; } public Point() { this(0.0, 0.0); }..... }

6 The toString method l It is very common to print the content of an object. l Thus, when designing a class, a toString method that returns the string representation of the object is usually provided. l If the toString method is provided for a class, Java automatically calls it whenever an object of the class is used where a string is expected. Example in a printing or concatenation operations. l If a toString method is not provided, the toString method of the standard class Object is used – This is not very useful as it returns a string in the form Where ClassName is the name of the class from which the object is created and memory address is the location of the object in the memory. l It is thus, very useful to always provide a toString method for each class we define.

7 The equals method l Sometimes we would like to compare between contents of two objects l Since the == operator only compares references of two objects, it is usually necessary to provide an equals method for a class. l If an equals method is not provided, the equals method of the standard Object class is used. l However, this is useless because it compares only references. l Thus, again it is important that we implement an equals method for each class we design. l The next example modifies the Point class by providing the equals and toString methods. public class Point { private double x; private double y; public Point(double x, double y) { this.x=x; this.y=y; } public Point() { this(0.0, 0.0); }

8 The equals method (cont’d) 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 x, double y) { this.x=x; this.y=y; } public void translate(double dx, double dy) { x += dx; y += dy; } public double getX() { return x; } public double getY() { return y; } public String toString() { return "("+x+", "+y+")"; } public boolean equals(Point p) { return (this.x == p.x && this.y == p.y); }