Successfully reported this slideshow.

Segundo avance


Published on

  • Be the first to comment

  • Be the first to like this

Segundo avance

  2. 2. Technical University of Manabi mission:Being academics, scientists and professionals responsible, humanistic, ethicaland caring, committed to the goals of national development, which contributeto solving the countrys problems as university teaching with research, able to generate and apply new knowledge, encouraging the promotion and dissemination of knowledge and culture, under the Constitution of the Republic of Ecuador. visio n: Being university, leader and reference of higher education in Ecuador, promoting the creation, development, transmission and dissemination of science, technology and culture, social recognition and regional and global projection. FACULTY OF COMPUTER missio n: Being a unit with high academic prestige, efficiency, transparency and qualityin education, organized in their activities, protagonists of regional and national progress. visio n:Being efficient and innovative professionals in the field of computer science,with honesty, fairness and solidarity, provide answers to the needs of society by raising their standard of living. Inheritance in java
  3. 3. IntroductionHeredity, understood as a feature of object-oriented programming and more specifically the C+ +, to define a class by modifying one or more classes and changes usually consistexistentes.Estas add new members (variables or functions), to the class being defined,although it can redefine variables or class member functions existentes.La and is part of thisprocess is called base class and the new class that is obtained is called derived classThis in turn may be based on a new class derivation process, thus initiating a class hierarchy.It is one of the key weapons of OOP. Allows you to create new classes that inheritcharacteristics show in previous classes. This greatly facilitates the work that has createdstandard classes for all programmers and from them create our own personal lessons. This ismore convenient than having to create our classes from cero.Para a class to inherit thefeatures of another must use the extends keyword after the class name. A word is followed bythe name of the class whose characteristics are inherited. You can have only one classinheritance (a class inherits laque is called superclass and inherited class is called subclass).Example:The idea of inheritance is to allow the creation of new classes based on existing classes.When you inherit from an existing class, we reuse (or inherit) methods and fields and add newfields and methods to meet the new situation.Every time we find the relation "is-a" between two classes, we are in the presence ofinheritance.The existing class is called the superclass or base class, or parent class.The new class is called subclass, derived class, or child class.Through inheritance we can add new fields, and we can add or set methods (override). Onmounting a method is redefining the inherited case.The process of inheritance is transitive
  4. 4. Types of inheritanceSingle inheritance• A class can inherit from a single• Class.Example: Java, C #Multiple Inheritance• A class can inherit from multiple classes.• Classes form a directed acyclic graphExamples: Eiffel, C + +
  5. 5. Advantage• Helps programmers code and saving time, since the parent class has been implemented andverified earlier, deducting only referenced from the derived class to the base class (typicallyextends, inherits, subclass or other similar keywords, depending on the language).• Objects can be constructed from the like. To do this there needs to be a base class (whichcan even be part of a broader class hierarchy).• The derived class inherits the behavior and attributes of the base class, and it is common foryou to add your own behavior or alter the inherited.• Any class can serve as a base class to create others.Inheritance and information hidingIn some languages, the designer can define instance variables and methods of the objects of aclass are visible. In C + + and Java this is accomplished with the specifications private,protected and public. Only variables and defined as public methods on an object will be visiblefor all objects. In other languages like Smalltalk, all instance variables are private and allmethods are public.Depending on the language used, the designer can also control which members of thesuperclass are visible in subclasses. In the case of Java and C + + access specifiers (private,protected, public) of the members of the superclass also affect inheritance:• Private: no private member of the superclass is visible in the subclass.• Protected: protected members of the superclass are visible in the subclass, but not visible tothe outside.• Public: public members of the public remain superclass in the subclass.APPLICATION EXAMPLEConsider a simple example of inheritance, for this case we will create a class which is inheritwhere the surname and assign another class where the name of an "X" person. First lets lookat the class to inherit:package inheritance;
  6. 6. ClaseHeredada {public class String last; public ClaseHeredada (String Data) { this.Apellido = data;}}Once we have the class that we inherit our class that will contain the name of the "X" andassign the following code:package inheritance;public class extends Inheritance {ClaseHeredada String name; public Inheritance (String text) { super (text); } public void setPersona (String NombrePer) { NombrePer + = this.Apellido; } getPersona public String () { return Name; }}It extends to indicate that we are inheriting class "ClaseHeredada" where the surname. Thesuper is to indicate that we are instantiating the class constructor we are inheriting andmethods are getPersona setPersona and with which will capture and take back the name ofthe person.After this process, we now use in our main instantiating class as follows:
  7. 7. public static void main (String [] args) { Inheritance Inheritance X = new ("Arias Figueroa"); X.setPersona ("Kevin Arnold"); System.out.println (X.getPersona ());}Instantiate Class "Inheritance" passing to its constructor the name of the person which willinternally what the upper class "ClaseHeredada", then we assign the name using his methodsetPersona and print it using getPersona.Made this code should print the persons name by concatenating the name of it.AnnexesDrill1) Basic example of inheritance in Java. In this case the Citizen class inherits from the Personclass to take and extend functionality.Human class { protected String name; protected String name; public Human (String name, String name) { = name; this.apellido = name; } public String FullName () { return this.apellido + "," + this.nombre; } identifying public String () { this.nombreCompleto return (); }}
  8. 8. Citizen class Human extends { protected String document; public City (String name, String name, String document) { super (name, name); this.documento = document; } identifying public String () { super.identificacion return () + "document" + this.documento; }}Inheritance {public class public static void main (String args []) { Human = new Human ("Emilio", "Rosso"); Citizen b = new City ("Emilio", "Rosso", "3052454545"); Human [] arregloDeHumanos; arregloDeHumanos.push (a); arregloDeHumanos.push (b); identificarPolimorfico (arregloDeHumanos); } public static void identificarPolimorfico (Human [] arregloDeHumanos) { for (int i = 0; i <arregloDeHumanos.length; i + +) {
  9. 9. System.out.println ("Identifying" + arregloDeHumanos [i]. Identification ()); } }}But besides this compositional technique is possible to think of cases where a class is anextension of another. That is, a class is like another and also has some sort of characteristicthat distinguishes it. For example we might think of the Employee class and define it as:class Employee { String name; int numEmpleado, salary; private static int counter = 0; Employee (String name, int salary) { = name; this.sueldo = salary; numEmpleado = + + count; } public void aumentarSueldo (int percentage) { salary + = (int) (salary * increase / 100); } public String toString () { return "Num employee" + numEmpleado + "Name:" + name + "Salary:" + salary; }}