1 / 77

Object Oriented Programming

Object Oriented Programming. ICS 4C / 4U. (PARENT)CLASS this is a class of types of Birds . (SUB)CLASS This is the Roadrunner class . It tells you how to build a roadrunner when instructed to do so. . LOCAL VARIABLE

adair
Télécharger la présentation

Object Oriented Programming

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. Object Oriented Programming ICS 4C / 4U

  2. (PARENT)CLASS this is a class of types of Birds

  3. (SUB)CLASS This is the Roadrunner class. It tells you how to build a roadrunner when instructed to do so. LOCAL VARIABLE The 'flying' method requires a new variable unique to the flying method called "hopping height". Because "hopping height" is only used in this one method, and its value dies once the method has been executed, then it is considered a local variable.

  4. Notice that because roadrunners prefer to run than fly, they are treated differently. • The miles per hour it runs is a property unique to roadrunners. • Also, since its flying is made up of hops and flaps, then its flying method must override the typical flying method of other bird objects in the bird class.

  5. CLASS A Class is a template used to describe all of the objects common to that class by defining all the properties of its objects as well as what messages (called methods/funct/ procedures) the object is to respond to. Classes in other languages may be referred to as factories, object types, templates etc. Classes are indicated by the first letter being capitalized as in Roadrunner or Bird.

  6. SUBCLASS ('derived class, child class or descendant')and PARENT CLASS ('base class,superclass, ancestor') Roadrunner is a subclass of its parent class, Birds. The Roadrunner class is a blueprint that describes a hypothetical roadrunner. It is understood that a subclass will inherit all the properties(member data) and methods of its parent class. As well, additional properties unique to the subclass may be added if needed (i.e. miles per hour it runs).

  7. A subclass can also override its parent's methods by simply using the same name, but redefining the method (i.e. the flying method). The process of creating subclasses is called 'designing by difference' and is useful when you have large libraries. Designing by difference allows you to create a new class by simply defining the parts of it that are different from the parent class.

  8. When a message is sent to an object to invoke a method, the object finds the most recent ancestor class that implements the method and invokes that method. i.e. sending the Roadrunner instance a flying message will use the overridden method found in the roadrunner class, and not the flying method of the bird class.

  9. OBJECTS An object created from a class is often referred to as an instance of a class or it is said to have been instantiated from a class. An object is made up of properties and methods.

  10. MEMBER DATA ('properties, variables') Member data refers to the data stored within an object or class. Member data within an instantiated object is called instance variables. You define which member variables are part of a class in the class definition and new instances of these variables are created every time a new object is created from the class. Every instance of a class will have the same types of variables contained within it, but what's stored in those variables will be different from one object to another.

  11. An object that contains only member data can be thought of as being functionally very similar to a traditional C struct or Pascal / Turing RECORD data structure. In Java, if the class doesn't specify the value to be stored in an instance variable, it will be set to zero. Variables, (local or member) unlike classes, are written entirely in small letters.

  12. LOCAL VARIABLES If a variable is found within a method and nowhere else, such as the hopping height variable of the Roadrunner class, then it is a local variable whose value dies once the method has been executed.

  13. METHODS('functions/procedures') Methods are procedures(return no value) or functions(return a value) that are instructions called into action.

  14. 'CALLING' A METHOD ('sending a message, invoking a method, executing a message') Calling a method is like calling a function /procedure into action. Calling a method is also referred to as sending a message to the object that contains the method. i.e. calling the flying method of the cardinal object is referred to as sending a flying message to that targeted object.

  15. When a method is invoked (or 'called'), any reference to a member variable in the methods code will actually deal with the instance variable contained in the object of which the method is a member.

  16. ENCAPSULATION Member variables can be hidden from code in other objects or classes. For example, number of eggs laid variable could be specified as hidden or private to the cardinal object. This process is called encapsulation.

  17. ABSTRACTION Abstraction is the process of identifying a general-purpose class that contains common properties and methods that you want to use as the parent class for a number of related classes.

  18. REFERENCES('pointers') Most of the memory allocated in a Java program is in the form of objects instantiated from classes. When an object is created, it is assigned a reference. This reference is a special type of value that you use whenever you want to perform any sort of action on the object.

  19. You typically store this object reference in a variable, so that you can use the object again later in your program. This reference variable is like a 'pointer' in C or C++ except that you can't control Java's memory locations manually like you can in C or C++. Pointers in C are very powerful, allowing a program to run very quickly, but they also have their disadvantages in that they are complicated and they allow for memory leaks.

  20. GARBAGE COLLECTION When an object is no longer in use (i.e. no references to the object are stored in any variables anywhere in your program), the object is automatically disposed of and the memory it used is free for use by other objects. This process of freeing unused objects is referred to as garbage collection. This process is automatically done, whereas in C or C++, you have to manually free up memory by using the free command. If you don't free up memory, you may end up with a memory leak.

  21. CONSTRUCTORS What is a constructor? • a method • the method has the same name as the class • used at the beginning of your class to set default values. • The "default" constructor is the one with nothing in the brackets (no parameters).

  22. Other constructors also have the same name as the class but will differ by either the number of parameters or the type of parameters (I.e. int/String/float). • There is no return type, since nothing is returned, so you will not see the words: void, float, int, String etc. • The word "static" is also not used.

  23. member (global) variables are declared here. Eg. class Box{ intheight; intwidth; intrepetitions; public Box( ) { height=10; width=10; repetitions=3; } If there is nothing in the bracketsthen this is the “default constructor”.

  24. public Box(int h, int w) { height=h; width=w; repetitions=3; } This constructor allows the user to pass in their own values for height and width

  25. public Box(int h, intw, int r) { height=h; width=w; repetitions=r; } This constructor allows the user to pass in their own values for height and width AND repetitions.

  26. OVERRIDING VARIABLES When writing up your driver class that defines your objects, it really is undesirable to override a variable by: objectname.variablename = newValue; i.e. redCat.colour=Color.red; or redCat.x=70; That is because you would have to know the variable name found in the template class.

  27. This is considered an invasion of privacy, and allows for no protection of variables because they can then be overridden. It is far better to create a method in your template that allows you to pass in a value that can then be used later on in the template when the corresponding method is called. Remember, the name of the parameter/argument passed in through the call line, needn't be the same name as the parameter found in the actual method, for portability and privacy's sake.

  28. Bad Approach Driver class.... object created called redCat redCat.x = 120; Template class.... x=100; //default value

  29. Desired Approach Driver class... object created called redCat redCat.setXValue(120); Template class.... x=100; //default value public void setXValue(intnewX){ //***** x=newX; }

  30. **this method is called a "mutator" method as a mutator can modify an object, in this case, it's just assigning a new value to one of the global variables ("fields"). Often these methods start with the word "set". There are also "accessors" which are methods that accesses the contents of an object without changing it, such as returning the value of one of the variables. Often these method names start with the word "get".

  31. "THIS" EXPLAINED The keyword "this" has about 4 different uses: • as a constructor: this() • as a variable: this.variablename • as a method: this.methodname() • as an argument: nameofmethod(this, other arguments too if needed)

  32. 1. CONSTRUCTOR Let's assume that a class has 2 or more constructors. "This" means use the current constructor (as well as any additional lines that may follow). public class Robot{ intspeed; public Robot(){ intunitsmove=1; String direction="right"; }

  33. 1. CONSTRUCTOR Let's assume that a class has 2 or more constructors. "This" means use the current constructor (as well as any additional lines that may follow). public class Robot{ intspeed; public Robot(){ intunitsmove=1; String direction="right"; }

  34. pubic Robot(inthowfast){ this(); speed=howfast; } "this()" means that you substitute the above default constructor (i.e. intunitsmove=1; and String direction="right") etc.

  35. 2. VARIABLE When using more than one class, you may want to use the same variable name in both classes. For example, we are so used to using the variable "c" for the Console class, that to change the name would only pose confusion. Similarly, the terms "x" and "y" are so frequently used to describe axis and co-ordinates that to use "p" and"q" would only pose confusion.

  36. So, when passing a variable from one class into another class, the word "this" is used to distinguish the one variable from another with the same name. "This" is usually used to describe the variable in the parent class, the current class, whereas NO "this" is used for the variable passed in from another class.

  37. Driver class main method Console c=new Console(); Another d=new Another(); d.methodname(c); no "this" used since different variable names are used (c & cons) passed in Another class Console cons; ... methodname(Console c){ cons=c;

  38. Driver class main method Console c=new Console(); Another d=new Another(); d.methodname(c); "this“ is used since both classes have the same variable name for the Console class: c passed in Another class Console c; ... methodname(Console c){ this.c=c;

  39. 3. METHOD Similar to above, you may have two classes that have the same method name, although the 2 methods are entirely different. And so "this" would mean use the method from the current class, and not from another class (i.e. the parent class).

  40. 4. PARAMETER When "this" is used as an argument of a method, then it means that it is passing a reference to the current object. The two program segments below behave identically. (Keep in mind, that there may be more than one argument in any given method, so the other arguments would remain the same.) For example, the program on the next slide has a method of the Graphics class that assumes its sole parameter is a reference to the current object (which would access the parent class, Applet).

  41. with "this" class Robot extends Applet{   pubic void paint(Graphics g){ g.methodname(this); }

  42. without "this" class Robot extends Applet{ Applet apple = new Applet(); public void paint(Graphics g){ g.methodname(apple); }

  43. SETTING THIS AND THAT Quite often you will see methods that start with the word "SET". The methods are seemingly short, often containing just 1 line. Why do we use these "set" such-and-such methods? So we can avoid having to pass variables into every single method that needs them and to protect template classes from having too many methods and variables tampered with.

  44. import Box; import java.awt.*; public class Driver{ public static void main (String [ ]args){ Console c=new Console(); Box square=new Box(); //declare & instantiate your Box object named square Box bigBox=new Box(100); //declare & instantiate Box object named bigBox square.setConsole(c); square.drawBox(30); bigBox.setConsole(c); bigBox.setTheColor(Color.green); bigBox.drawBox(40); } // end the main method } // end the Driver class

  45. import java.awt.*; public class Box{ inttopLeft=5; inttopRight=10; Console c; Color boxColor; intwidth; public Box( ){ // default constructor setSize(10); // if you forget to set a size, then it defaults to 10 boxColor=Color.red; // if you forget to set a color, then it defaults to red } // end Box (default) constructor

  46. public Box(int width){ // constructor with 1 int parameter setSize(width); // whatever width you pass in, becomes the width boxColor=Color.red; // if you forget to set a color, then it defaults to red } // end Box public void setSize(int w){ width=w; // let the width(width) be whatever value is passed in (w) } // end setSize method public void setConsole(Console c) { this.c=c; // let the "c" in the current(this) Box class be the same // value as the "c" passed in from the Driver class } // end setConsole method

  47. public void setTheColor(Color newColor){ boxColor=newColor; //let the boxColor be whatever value is passed in(newColor) } // end setColor method public void drawBox(int width){ c.setColor(boxColor); c.drawRect(topLeft,topRight, width, width); } // end drawBox method }// end Box class

  48. WAYS TO MAKE THE CONSOLE CLASS GLOBAL TO EVERY CLASS If you want to use one of Console's methods (I.e. c.println) in more than one method in the Driver class then you must make it a global variable by putting static Console c=new Console(); before the main method . The reason why you must include the word static, is because the main method and hence other methods in the same class are all static.

More Related