1. TECHNICAL UNIVERSITY OF MANABÍ
FACULTY OF COMPUTER SYSTEMS
ENGINEER CAREER
ENGLISH PROJECT:
PROGRAMMING 2
NAME:
AYALA CARRILLO JUAN
ALBERTO
COURSE:
3 SEMESTER C
DELIVERY DATE:
21/ 01/2012
2. Technical University of
Manabi mission:
Being academics, scientists and professionals responsible, humanistic, ethical
and caring, committed to the goals of national development, which contribute
to solving the country's 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 quality
in 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. Introduction
Heredity, 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 consist
existentes.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 this
process is called base class and the new class that is obtained is called derived class
This 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 inherit
characteristics show in previous classes. This greatly facilitates the work that has created
standard classes for all programmers and from them create our own personal lessons. This is
more convenient than having to create our classes from cero.Para a class to inherit the
features of another must use the extends keyword after the class name. A word is followed by
the name of the class whose characteristics are inherited. You can have only one class
inheritance (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 new
fields and methods to meet the new situation.
Every time we find the relation "is-a" between two classes, we are in the presence of
inheritance.
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). On
mounting a method is redefining the inherited case.
The process of inheritance is transitive
4. Types of inheritance
Single 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 graph
Examples: Eiffel, C + +
5. Advantage
• Helps programmers code and saving time, since the parent class has been implemented and
verified earlier, deducting only referenced from the derived class to the base class (typically
extends, 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 (which
can even be part of a broader class hierarchy).
• The derived class inherits the behavior and attributes of the base class, and it is common for
you to add your own behavior or alter the inherited.
• Any class can serve as a base class to create others.
Inheritance and information hiding
In some languages, the designer can define instance variables and methods of the objects of a
class 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 visible
for all objects. In other languages like Smalltalk, all instance variables are private and all
methods are public.
Depending on the language used, the designer can also control which members of the
superclass 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 to
the outside.
• Public: public members of the public remain superclass in the subclass.
APPLICATION EXAMPLE
Consider a simple example of inheritance, for this case we will create a class which is inherit
where the surname and assign another class where the name of an "X" person. First let's look
at the class to inherit:
package inheritance;
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" and
assign 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.name = this.Apellido;
}
getPersona public String () {
return Name;
}
}
It extends to indicate that we are inheriting class "ClaseHeredada" where the surname. The
super is to indicate that we are instantiating the class constructor we are inheriting and
methods are getPersona setPersona and with which will capture and take back the name of
the person.
After this process, we now use in our main instantiating class as follows:
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 will
internally what the upper class "ClaseHeredada", then we assign the name using his method
setPersona and print it using getPersona.
Made this code should print the person's name by concatenating the name of it.
Annexes
Drill
1) Basic example of inheritance in Java. In this case the Citizen class inherits from the Person
class to take and extend functionality.
Human class {
protected String name;
protected String name;
public Human (String name, String name) {
this.name = name;
this.apellido = name;
}
public String FullName () {
return this.apellido + "," + this.nombre;
}
identifying public String () {
this.nombreCompleto return ();
}
}
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. System.out.println ("Identifying" + arregloDeHumanos [i]. Identification ());
}
}
}
But besides this compositional technique is possible to think of cases where a class is an
extension of another. That is, a class is like another and also has some sort of characteristic
that 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) {
this.name = 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;
}
}