Class 6


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Class 6

  1. 1. Kenia Álava
  2. 2. CONCEPTS OF OBJECT-ORIENTED PROGRAMMING (POO)1 What is POO?Is to develop programs from objects. These objects waiting messages, process and sendmessages to other objects. In object-oriented programming, objects are defined thatmake up an application. These objects are formed by a series of features and operationsthat can be performed on them.2 What is an object?The answer to this question in terms outside the programming seems simple. An object isa person, animal or thing. It is distinguished from other objects to have certaincharacteristics and is good for something, or put another way, you can perform variousoperations with or on that object. For example:A house is an object.FEATURES:Number of floors, Total height in meters, the facade color, number of windows, doornumber, city, street and number where it is located, etc..OPERATIONSBuild, destroy, painted facade, modify any of the characteristics, such as opening a newwindow, etc.Obviously, each object can be defined in terms of a multitude of features and an extensivearray operations. Now in terms of programming, the programmer mission will determinewhat features and interest to maintain operations on an object. For example, on the homeobject may not be necessary to know its location and therefore, these features are notpart of the object defined by the programmer. The same could be said about theoperations. In terminology of object-oriented programming, the characteristics of theobject are called attributes and operations METHODS. Each of these methods is aprocedure or function belonging to an object. Kenia Álava
  3. 3.  INFORMAL TERMINOLOGYFEATURESOPERATIONS (PROCEDURES AND FUNCTIONS)  FORMAL TERMINOLOGYATTRIBUTESMETHODSAn object is formed by a number of features or data (attributes) and a set of operations(methods). No conceivable only on the basis of data or operations but as a whole.Example: Think about what methods would be needed to make a car turn the key EnterTurn the key to the electrical system This shareholding ignite engine3. Classes and ObjectsIn OOP we must distinguish between two closely related concepts, the class and object.Analogous to how to define variables in a programming language, when you declare anobject must define the type of object to which it belongs. This type is the class.In C, we define two variables X and Y integer as follows:int X,Y;In this case, X and Y are variables and the type of these variables is integer. The way todeclare objects in Java is the same:Ccasa casa1,casa2;Here are effectively house2 casa1 and variables, but rather special, are objects.Furthermore, the type of objects is Ccasa. This type is the class of the objectAnalogyVariable → Object(X,Y) (casa1, casa2) Kenia Álava
  4. 4. Type → Class(int) (Ccasa)Declaring casa1 and house2 as objects belonging to the class Ccasa, it indicates thathouse2 casa1 and have a number of attributes (data) and are nPuertas, nVentanas andcolor, and also a number of methods (operations that can be performed on them) as:abrirVentanas (), cerrarVentanas (), etc..4. Properties must meet to be considered a language object oriented.INHERITANCEPOLYMORPHISMENCAPSULATIONEncapsulation is the property possessed objects hide their attributes, and even methodsto other parts of the program or other objects. The natural way to builda class is to define a set of attributes which generally are not accessible outside of thesame object, but can only be modified by the methods which are defined as accessiblefrom the outside of that class.class Ccasa {int nPuertas,nVentanas;String color;public Ccasa (int np, int nv, String co) {nPuertas=np;nVentanas=nv;color=co;}public void pintar(String co) {color=co;}public void abrirVentanas(int n) {nVentanas=nVentanas+n;}public void cerrarVentanas(int n){nVentanas=nVentanas-n;if (nVentanas<0)nVentanas=0;} Kenia Álava
  5. 5. public void abrirPuertas(int n) {nPuertas=nPuertas+n;}public void cerrarPuertas(int n){nPuertas=nPuertas-n;if (nPuertas<0)nPuertas=0;}}//AQUI DECLARACIÓN DE OBJETOS E INSTANCIASCcasa casa1,casa2;The normal way to declare the class Ccasa is to define a set of attributes that are notaccessible from anywhere in the program, but only through certain methods. So, if youopen a new window in the house casa1, traditional philosophy of a programmer would dothe following:casa1.N_VENTANAS = casa1.N_VENTANAS + 1;However, the natural way to do it in OOP is calling the method:casa1.abrirVentanas(1);That method (procedure) will increase by 1 nVentanas attribute. This does not mean thatthe attribute can not be accessed nVentanas the traditional way (if it had been defined aspublic, but for that language may be regarded as OOP, should allow for the possibility ofprohibiting access to the attributes directly.Inheritance.One of the main advantages of OOP. This property allows you to define other descendantclasses, so that the new class (the child class) inherits from the parent class all itsattributes and methods. The new class can define new attributes and methods can evenredefine existing attributes and methods (for example, change the type of an attribute oroperations performed by a given method.) Is the natural way to define objects in real life.Most people would say, for example, that a house is a house with a garden. It has thesame characteristics and properties or operations you can perform on a house and alsoincorporates a new feature, the garden. At other times, they added functionality(methods) and attributes. For example: a duck is a bird that nothing. Kenia Álava
  6. 6. It has the same characteristics as the birds and should only declare a method on the newclass (the swim method).This property enables code reuse and is very easy to take advantage of the existing classcode, modifying them minimally to fit the new specifications.example:Suppose we have constructed Ccasa class and we want to define a new class thatrepresents the chalets. In this case you may want to define a new attribute thatrepresents the square meters of garden. Instead of redefining a new class from scratch,can be used for writing code Ccasa class as follows. Kenia Álava
  7. 7. PolymorphismPolymorphism allows the same message to objects of different classes do these alsobehave differently (different objects can have methods with the same name or the sameobject methods can have identical names but different parameters)class Ccasa {public Ccasa(int np, int nv, String co) {nPuertas=np;nVentanas=nv;color=co;}public Ccasa() {nPuertas=0;nVentanas=0;color=””;} }You have two methods with the same name but different parameters. In the first case, theattributes of the object initialized with the parameters of the method and in the secondcase will be initialized to zero, for example. Also, if you have two objects and chalet1 casa1and calls the method: chalet1.abrirVentanas (2)Will execute the code of the class abrirVentanas procedure Cchalet and Ccasa class. Kenia Álava