Docsity

Lenguajes de programación práctica 1, Esquemas y mapas conceptuales de Lenguajes de Programación

Toggle sidebar
bg1
Universidad Autónoma de Nuevo León
Facultad de Ingeniería Mecánica y Eléctrica
Lenguajes de programación
Actividad fundamental 1 (INHERITANCE &
POLYMORPHISM)
Nombre: Omar Juvenal Ramírez Gutiérrez
Matrícula: 1981374
Brigada: 003
Docente: Daniela Fernanda Gamboa Alonso
30/04/2023
pf3
pf4
pf5
pf8
pf9
pfa

Vista previa parcial del texto

¡Descarga Lenguajes de programación práctica 1 y más Esquemas y mapas conceptuales en PDF de Lenguajes de Programación solo en Docsity!

Universidad Autónoma de Nuevo León

Facultad de Ingeniería Mecánica y Eléctrica

Lenguajes de programación

Actividad fundamental 1 (INHERITANCE &

POLYMORPHISM)

Nombre: Omar Juvenal Ramírez Gutiérrez

Matrícula: 1981374

Brigada: 003

Docente: Daniela Fernanda Gamboa Alonso

Interface declarations can define a default implementation of their members. These implementations are inherited by derived interfaces and classes that implement those interfaces. For more information on default interface methods, see the article on interfaces. When a class is defined to derive from another class, the derived class implicitly gets all the members of the base class, except for its constructors and finalizers. The derived class reuses the code of the base class without having to reimplement it. You can add more members in the derived class. The derived class extends the functionality of the base class. The illustration below shows a WorkItem class that represents a work item of a business process. As with all classes, it is derived from System.Object and inherits all of its methods. WorkItem adds six members of its own. These members include a constructor, because constructors are not inherited. The ChangeRequest class inherits from WorkItem and represents a particular type of work item. ChangeRequest adds two more members to the members it inherits from WorkItem and Object. It must add its own constructor and also adds originalItemID. The originalItemID property allows the ChangeRequest instance to be associated with the original WorkItem to which the change request applies. Diagram showing class inheritance

The following block shows how to use the base and derived classes:

C#

Derived classes Base Class Vs. Derived Class: Explore the Difference between Base Class and Derived Class in C++ In the world of object-oriented programming languages, both base and derived classes play an important role. The base class is the existing class whereas the derived class is one that acquires the properties of a base class. There are many differences between them, let’s explore some major differences between a base class and a derived class. What is a Base Class? In an object-oriented programming language, a base class is an existing class from which the other classes are determined and properties are inherited. It is also known as a superclass or parent class. In general, the class which acquires the base class can hold all its members and some further data as well. Syntax: Class base_classname{ … }. What is a Derived Class? A derived class is a class that is constructed from a base class or an existing class. It has a tendency to acquire all the methods and properties of a base class. It is also known as a subclass or child class. Syntax: Class derived_classname : access_mode base_class_name { … }. Difference between Base Class and Derived Class in C++

Note: Methods in an interface (see the Interfaces section) that are not declared as default or static are implicitly abstract, so the abstract modifier is not used with interface methods. (It can be used, but it is unnecessary.) Abstract Classes Compared to Interfaces Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public. In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces. Which should you use, abstract classes or interfaces? Consider using abstract classes if any of these statements apply to your situation: You want to share code among several closely related classes. You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private). You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong. Consider using interfaces if any of these statements apply to your situation: You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes. You want to specify the behavior of a particular data type, but not concerned about who implements its behavior. You want to take advantage of multiple inheritance of type. An example of an abstract class in the JDK is AbstractMap, which is part of the Collections Framework. Its subclasses (which include HashMap, TreeMap, and ConcurrentHashMap) share many methods (including get, put, isEmpty, containsKey, and containsValue) that AbstractMap defines. An example of a class in the JDK that implements several interfaces is HashMap, which implements the interfaces Serializable, Cloneable, and Map<K, V>. By reading this list of interfaces, you can infer that an instance of HashMap (regardless of the developer or company who implemented the class) can be cloned, is serializable (which means that it can be converted into a byte stream; see the section Serializable Objects), and has the functionality of a map. In addition, the Map<K, V> interface has been enhanced with many default methods such as merge and forEach that older classes that have implemented this interface do not have to define. Note that many software libraries use both abstract classes and interfaces; the HashMap class implements several interfaces and also extends the abstract class AbstractMap. An Abstract Class Example In an object-oriented drawing application, you can draw circles, rectangles, lines, Bezier curves, and many other graphic objects. These objects all have certain states (for example: position, orientation, line color, fill color) and behaviors (for example: moveTo, rotate, resize, draw) in common. Some of these states and behaviors are the same for all graphic objects (for example: e,

fill color, and moveTo). Others require different implementations (for example, resize or draw). All GraphicObjects must be able to draw or resize themselves; they just differ in how they do it. This is a perfect situation for an abstract superclass. You can take advantage of the similarities and declare all the graphic objects to inherit from the same abstract parent object (for example, GraphicObject) as shown in the following figure. Objects and Messages Smalltalk is what is known as an Object Oriented Programming Language. When you start your Dolphin image you bring into your computer's memory a sea of interconnecting and communicating objects. This chapter introduces the ideas behind what objects actually are and how you can use messages to communicate with them. Please open a new workspace window using File/New so that you can try out the examples presented below. What is an Object? At the simplest level, an object is just an area of your computer's memory that contains some data bytes describing the object's state. For example, an object representing the text string, 'hello', would contain (amongst other things) the bytes: 104 101 108 108 111 Here each byte, of course, represents one character in the string. However, every object also contains additional information that allows it to "know" what kind of object it is. How this knowledge is encoded as part of the object is not relevant at this stage, but it is very important since it allows the object to determine how it should behave. For example, if we have two objects that are numbers it seems quite reasonable that we should be able to subtract one from the other. If, however, we had two text strings we would not expect to be able to perform a similar subtraction operation. The fact that the number objects "know" that they are numbers allows them to determine that subtraction is a relevant operation and also how that subtraction should be performed. In Dolphin, there are objects that represent (amongst other things):

new show exit includes: asSortedCollection You can use the Smalltalk language to create objects and send messages to them. Create a new workspace window (File/New) and try the following examples. You should display the result of evaluating each line individually. Tip: Remember that you don't have to select the entire line to do this, just place the insert cursor somewhere on the line and type Ctrl+D (Workspace/Display it) to cause it to be evaluated and the answer displayed. 3+ 'Hello world' reverse 5 factorial Simple Messages A Simple Message is one that requires no additional information or parameters apart from the message itself. Such messages must begin with a lower case letter and the remainder of the message keyword can consist of any letters or digits or the underscore (_) character. Try displaying the results of the following examples: 5 squared. Here the message squared is sent to the integer object 5. All messages return an answer of some description and, in this case, it will be 25. One great thing about Smalltalk is its ability to handle very large numbers of effectively unlimited precision. Try this: 200 factorial Here are some more message sends: 'mary had a little lamb' asUppercase The message asUppercase, when sent to a string object, causes a new string to be created with the contents of the original converted to upper case letters. This new string is then answered as the result of the message. #(3 4 5 6 7) size The message size, when sent to this array object containing five integers, answers the number of elements in the array, in this case, 5. The object to which a message is sent is called the receiver. The message itself is sometimes called a message selector (because it selects which operation will be performed) and the result is generally referred to as the answer.

Messages that are not understood We mentioned before that an object will only respond to a certain set of messages depending on what class of object it is. For example, objects that are kinds of number will hopefully handle arithmetic messages successfully. We would not, however, really expect text string objects to behave in the same way. Let's deliberately send a dubious message to a string: 'Hi Andy' squared Notice that this causes a walkback window to pop-up. Dolphin uses these windows to indicate when some sort of error has occurred. The window caption indicates the basic problem, in this case, String does not understand #squared. At this stage, you could choose to enter the system debugger (by pressing the Debug button) to further diagnose the problem. However, understanding the debugger is perhaps beyond us at this point so, for the time being, get used to clicking on Terminate when you see a walkback pop up. Sending Messages on top of Messages When you send a message to a receiver, you always receive another object back as the answer. It seems perfectly reasonable that you should be able to send further messages to this answer. Try: 20 factorial displayString Here, the message displayString, is sent to the result of 20 factorial. Look carefully, and you'll see that what is displayed is surrounded by single quote marks, thus indicating that the integer result has been converted to a text string representation. Perhaps we might additionally want to know how many characters are in the display string for 20 factorial: 20 factorial displayString size So you can see that you can use sequences of messages to create more complex expressions. Binary Messages Sometimes, a message needs additional information to perform its work. Certain messages, called Binary Messages, require two objects; the receiver and one parameter. Try: 3 + 4 6 * 5 5 squared + 6 4 ** 3 The latter ** message selector represents exponentiation, so this expression computes 4 to the power 3. Here, the integer object 4 is the receiver and 3 is the single parameter required by the ** message.