Chapter5_2-Methods

x object kasree 0 1 bast maquam b the constructor b

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: OOP Class with Multiple Constructors public class Kasree { private int bast; private int maquam; A. The constructor A. The constructor A. declared with no-parameter declared with no-parameter is used to create the object is used to create the object public Kasree() { bast = 0; maquam =1; } public Kasree(int a, int b) { bast = a; if (b != 0) maquam = b; else maquam = 1; } ... x Object: Kasree 0 1 bast maquam B. The constructor B. The constructor B. declared with parameters is declared with parameters is used to create the object used to create the object y } Kasree x , y; x = new Kasree() y = new Kasree(4, 3); Code Page 35 Object: Kasree A A bast B B maquam 4 3 State of Memory Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Overloading • Two of the components of a method declaration comprise the method signature: • the method's name • and the parameter types. • The signature of the constructors declared above are: – Kasree() – Kasree(int, int) • overloading methods allows implementing different versions of the same method with different method signatures. • This means that methods within a class can have the same name if they have different parameter lists. Page 36 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Overloading (cont.) • Overloaded methods are differentiated by: • the number, • and the type of the arguments passed into the method. • You cannot declare more than one method with: • the same name, • and the same number and type of parameters. • The compiler does not consider return type when differentiating methods. • No declaration of two methods having the same signature even if they have a different return type. Page 37 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP...
View Full Document

This document was uploaded on 03/03/2014.

Ask a homework question - tutors are online