7/31/2019 Gua de Inicializacin (Fase 9) Herencia
1/23
H erencia de C lasesI.S.C. Dalia Domnguez Daz
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
2/23
I .S .C . D alia D ominguez D iaz
2
H erencia de C lases
La POO fac i l i ta vo lver a usar una c lase que creo para unp r o g r a m a d e n t r o d e o t r o , a h o r r a n d o t i e m p o yp rog ram ac in .
La POO in t roduce la capac idad de extender c lases,p roduc iendo nuevas de f in i c iones de c lases que heredant odo e l com por t am ien t o y cd igo de l a cl ase ex t end ida .
La herenc ia es l a capacidad de un a c lase der i v ada de h eredarlas caract er s t i cas de una c lase base ex is ten t e .
HERENCI A SI MPLE: Perm i te a una c lase der i vada hered arlas caract er s t i cas de una c lase base ya ex is t ent e .
HERENCI A MULTI PLE: A l p r oceso de u t i l i za r va r ias c lasesbase para der i v a r o t ra c lase
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
3/23
I .S .C . D alia D ominguez D iaz
3
H erencia de C lases
CLASE ORI GI NAL:
CLASE PADRE
CLASE BASE
SUPERCLASE
CLASE EXTENSI ON:
CLASE HI JA
CLASE DERI VADA
SUBCLASE
La extensin de una clase se denominaHERENCIA, la clase hija hereda todos los
atributos y mtodos de la clase padre que seextiende.
La palabra clave extendsse usa para generar unasubclase (especializacin) de un objeto.
Con la palabra superaccedemos a los miembrosde la superclase desde la subclase
En Java no se puede hacer herencia ml tiple.
Para indicar que una clase deriva de otra, heredando sus propiedades(mtodos y atributos), se usa el termino extends, como el siguiente ejemplo;
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
4/23
I .S .C . D alia D ominguez D iaz
4
E jemplos de extensiones de C lase
cl ass A lu m n o e x t en d s Pe r so n a { }
cl ass Em p l ea do e x t e n d s Pe r so n a { }
cl ass Pr o f eso r e x t e n d s Em p l ea d o { }
c la ss Su p e r v i so r e x t e n d s Em p l ea d o { }
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
5/23
I .S .C . D alia D ominguez D iaz
5
S obre H erencia
Varios Miembros estn presentes en todas las clases
Una escuela tiene un sistema de control escolar que modela las siguientesclases:
Persona
String nombre
String curpint edad
getNombre()getCurp()getEdad()setNombre()
setCurp()setEdad()
Alumno
String nombreString curp
int edad
char grupochar semestre
getNombre()
getCurp()getEdad()setNombre()
setCurp()setEdad()getGrupo()getSemestre()setGrupo()setSemestre()
Catedratico
String nombreString curpint edad
float salarioString cedula
getNombre()getCurp()getEdad()
setNombre()
setCurp()setEdad()getSalario()getCedula()setSalario()setCedula()
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
6/23
I .S .C . D alia D ominguez D iaz
6
S obre H erencia
Para reducir la aplicacin se usa la herencia
Persona
String nombreString curpint edad
getNombre()getCurp()getEdad()setNombre()setCurp()setEdad()
Alumno
char grupo
char semestregetGrupo()getSemestre()setGrupo()setSemestre()
Catedratico
float salarioString cedula
getSalario()getCedula()setSalario()setCedula()
Todos los miembros persona estn
disponibles para se Catedrtico y Alumno
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
7/23
I .S .C . D alia D ominguez D iaz
7
J erarqua de H erencia
Cualqu ier c lase en Java puede serv i r como base paraser ex ten d ida .
La c lase der ivada que se obt iene a su vez puede serex t end ida .
En java todas las c lases es tn der ivadas en una n icaj er ar qu a de h er en cia, p u est o q u e t od a clase o b ienheredada exp l c i t amente de o t ra c lase oim p l c i t am ente d e la clase ob jec t p rede f in ida .
Una ex tens in imp l c i t a ocur re cuando en ladec larac in de la c lase no se pon e un e x t e n d s
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
8/23
I .S .C . D alia D ominguez D iaz
8
L a palabra S uper
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
9/23
I .S .C . D alia D ominguez D iaz
9
I nvocando los C onstructores del P adre
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
10/23
I .S .C . D alia D ominguez D iaz 10
L a C lase O bject
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
11/23
I .S .C . D alia D ominguez D iaz 11
E jemplo 1 2 2
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
12/23
I .S .C . D alia D ominguez D iaz 12
H erencia de C lases
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
13/23
I .S .C . D alia D ominguez D iaz 13
H erencia de clases
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
14/23
I .S .C . D alia D ominguez D iaz 14
H erencia de C lases
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
15/23
I .S .C . D alia D ominguez D iaz 15
F ormas de H erencia
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
16/23
I .S .C . D alia D ominguez D iaz 16
C omposicin vs. H erencia
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
17/23
I .S .C . D alia D ominguez D iaz 17
H erencia vs. C omposicin
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
18/23
I .S .C . D alia D ominguez D iaz 18
C omposicin vs. H erencia
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
C
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
19/23
I .S .C . D alia D ominguez D iaz 19
H erencia vs. C omposicin
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
G t d b F it PDF C t F it S ft
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
20/23
I .S .C . D alia D ominguez D iaz 20
H erencia vs. C omposicin
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
G t d b F it PDF C t F it S ft
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
21/23
I .S .C . D alia D ominguez D iaz 21
E jemplo 1 2 3
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
Generated by Foxit PDF Creator Foxit Software
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
22/23
I .S .C . D alia D ominguez D iaz 22
E l M todo T oS tr ing
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
Generated by Foxit PDF Creator Foxit Software
7/31/2019 Gua de Inicializacin (Fase 9) Herencia
23/23
I .S .C . D alia D ominguez D iaz 23
E jemplo 1 2 4
Generated by Foxit PDF Creator Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.