1 / 29

Parameters… Classes Cont

Parameters… Classes Cont. Mrs. C. Furman October 13, 2008. Parameters. What are the primitive types? int, double, char, boolean (int, double, boolean on the AP)

Download Presentation

Parameters… Classes Cont

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. Parameters… Classes Cont Mrs. C. Furman October 13, 2008

  2. Parameters • What are the primitive types? • int, double, char, boolean (int, double, boolean on the AP) • Reference data types: objects. Because when we declare an object we create a reference that points to an object we call objects reference data.

  3. int i = 3; Date d = new Date (10, 13, 2008); Date today = d; Object Storage Date 3 i myMonth = 10 myDay = 13 myYear = 2008 d today Having 2 references to the same object is called aliasing! Can cause problems because the object can be changed through both d and today

  4. Null Reference Date tomorrow; • tomorrow is not initialized. • An uninitialized object variable is called a null reference. • We can test if a reference points to an object: if (tomorrow == null) • If it is referencing to an object, it can be set to null by: today = null; d = null; Automatic garbage collection!! Deallocation of memory!

  5. Formal vs. Actual Parameters • Formal Parameters: considered placeholders. These are the parameters found in the method signature. They contain the data type. • Actual Parameters: The parameters in the method call. Also called arguments. • NOTE: 1. The number of arguments must match the number of formal parameters • NOTE: 2. The type of the argument must match the corresponding parameters.

  6. Passing Primitives – Passing by Value • When a method is called, a new memory slot is allocated for each parameter. The value of each argument is copied into the newly created memory slot corresponding to each parameter. Ex. double doSomething (int value1, int value2) y = 5; z = 3; double x = ClassName.doSomething (y, z); y z x value2 value1 5 3 5 3 Deallocation of memory when doSomething is done!!

  7. Parameters… Continued • Any changes made to the parameters will not affect the values of the arguments in the calling program. When a method is exited, the local memory slot for the parameters are erased. public double doSomething (int value1, int value2) { double x; value1 = value1 + value2; x = value1 * 5.0; return x; } int y = 5, int z = 3; double x = ClassName.doSomething (y, z);

  8. Passing Objects as Parameters • When we pass with objects we are passing the reference to the object, not the object itself. And NOT a copy of the object. • It is possible to change the STATE of the object which the parameter refers to. Ex. int [] myList = {2, 3, 5}; public void something(int [] j) { j[1] = 100; } ClassName.something (myList); Will change myList to 2, 100, 5 2 3 5 100 myList j

  9. Inheritance • To design 2 or more entities that are different but share many common features. • 1st – define a class that contains the common features • 2nd – define classes that extend the common class and inherits everything from the common class. • The common class is called the superclass. (Ancestor) • The subclass (Descendent) is created from the superclass by absorbing its state and behaviors and then adding to them.

  10. Inheritance Hierarchy Person Superclass Student Parent Subclass Subclass Student and Parent are called siblings because hey are on the same level of inheritance hierarchy. Siblings DO NOT inherit from eachother. Student and Parent are “bigger” classes than Person. They have more data and methods. We say: Student is-a Person Parent is-a Person We are not limited to just one level of inheritance. Consider BoxBug….

  11. Inheritance with Grid World Actor Bug Rock Flower BoxBug CircleBug ZBug

  12. More SuperClass… SubClass • It is possible for a subclass to override the superclass. (Method Overriding) • We look to the subclass first, if we do not find what we need, then we look in the superclass.

  13. Format: public class Superclass { //private instance variables //constructors //methods } public class Subclass extends Superclass { //additional private variables //constructors (these are NOT inherited) //additional methods including those who might be //overridden. }

  14. Proctected Access Specifier • If we use protected in the superclass instead of private, the subclass can use the variables in the superclass directly.

  15. Method Overriding • Subclasses can override methods by defining a method with the same return type and signature as one in the Superclass. • Recall: Method signatures consists of the name and parameter types. • Partially Overridden methods: include a call to the superclass. [Usually when we want to do more than the superclass is already doing!]

  16. Super • super is a keyword. • used to call methods in the superclass, including the constructor. super.methodName (<arguments>); • We can create constructors using super() method. • super can only be used in the 1st line of the method / constructor • If no constructor is provided in the supclass, the compiler provides the following default: public SubClass() { super(); } • If the SuperClass has no default… compiler error.

  17. Rules for SubClasses • may not redefine public methods as private • may not override static methods of SuperClass • can not access private members of Superclass. (need to be protected.)

  18. Declaring • References of SuperClasses can refer to objects of SuperClasses or SubClasses, since the is-a relationship holds true. Ex. Person p = new Person(); Person s = new Student(); (a Student is-a Person!) We can not go the other way… Student s = new Person(); (Not all Person objects are Student objects.. they could be Parent)

  19. List Class • ArrayList class is a SubClass of the List Class. We can declare and create ArrayLists in the following way… List <Integer> numbers = new ArrayList<Integer> (); This is how you will see ArrayLists declared from now on…

  20. Polymorphism • A method which has been overridden in at least one subclass is said to be polymorphic. • Object references can also be polymorphic since they can refer to objects from different classes. • Ex. Person p = new Person(); p = new Student(); p = new Parent();

  21. Method Selection… Binding • Methods are selected based on the type of the ACTUAL OBJECT, not the reference type. Person p = new Parent(); Person s = new Student(); • s and p are both of type Person, but one is a Parent and one is a Student. • The selection of the appropriate method occurs at run-time. This is called dynamic binding or late binding.

  22. Selection of Overloaded Methods • Methods which are overloaded… same name, different parameter lists… are selected at compile time by comparing signatures. • This process is called Static Binding or Early Binding.

  23. Type Compatibility… Downcasting public class Animal { //constructor… public void eat(String food) { SOP (“Eating “ + food); } public void speak () { SOP (“Animal Sound”);} } public class Dog extends Animal { //constructor… public void wagTail() { SOP (“Wag.. Wag.. Wag”);} public void speak () { SOP (“bark bark”);} } 1. What method is being overridden? Given: Animal a = new Dog(); Dog d = new Dog(); Animal annie = new Animal(); 2. a.speak(); 3. annie.wagTail(); 4. d.wagTail(); 5. d.eat(“Carrot”); 6. a.wagTail();

  24. Answers… 1. speak is being overridden 2. At compile time we look at the Animal class. Since Animal can speak, this compiles fine. At runtime, we look at the Dog class, since a is a Dog, and we output… bark bark 3. annie is an Animal. Animals do not know how to wagTail. Error 4. d is a Dog. Dog’s can wagTail, so we output… Wag.. Wag.. Wag 5. Even though eat is not in the Dog class, we can call the eat method in the SuperClass, and output.. eating Carrot 6. a is an Animal. At compile time, we look at the Animal class. Animals can not wagTail, so we get an error… (wagTail is not polymorphic) how can we fix this.. ((Dog)a).wagTail(); We downcast a: cast a SuperClass to a SubClass.

  25. DownCast • At Compiler-time: a is a Superclass • At Run-time: a is a Subclass Run-Time: 1. Polymorphic if method is in the subclass 2. Inheritance if method is in the superclass. d.eat(“Carrot”); //Inheritance

  26. Class Cast Exception: • Casting an object to a class of which is it NOT an instance. Ex. Animal a = new Dog(); ((Cat)a).wagTail(); a is a Dog, not a Cat.

  27. Class and Instance Data Values • An object is comprised of data values and methods. • Instance Data Value: a value given to an object when it is created. It is not the same for all objects. Ex. starting balance for an Account • Class Data Value: used to represent information shared b all instances or to represent collective information about the instance. Ex. minimum balance required to be maintained in an Account.

  28. Class Data • Used to cut down on redundancy! Ex. minimum balance in a bank account is 100. If this is a class data value, it is coded into the class. If the bank wants to raise this amount to $200. It needs to be changed in one location.. the class. If we had made the minimum balance an Instance Data Value – we would need to change this value on every individual account!! Thousands of changes.

  29. Variables / Constants • Variable – A data value that can change throughout the program. • Constant – A data value that can not change later in the program. 4 kinds of data values: class variable class constant instance variable instance constant

More Related