Ch4-1 - EE322C DataStructures Chapter4:Inheritance...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Ramesh Yerraballi 4-1 EE 322C Data Structures Chapter 4:Inheritance (Part 1/2)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Ramesh Yerraballi 4-2 Inheritance in OO Programming Code Re-Use Generic Code : write your  code generically so that it  works on any type:  E.g.,  Sorting an array of  objects is independent of the  type of objects as long as  these objects are  comparable Inheritance : Extend  functionality to enhance or  restrict properties of the  original type Polymorhism Definition : Is the ability of  objects to respond to method  calls of the same name,  each once according to an  appropriate type-specific  behavior  It lets us: o treat derived objects just  like the parent objects o use an operator or method  in different ways for different  set of inputs(overloading)
Background image of page 2
Ramesh Yerraballi 4-3 Inheritance Inheritance models the  IS-A   relationship . A Circle IS-A Shape A Rectangle IS-A Shape A Square IS-A Rectangle A Car IS-A Vehicle Object is the Root of all  Classes Every Class IS-A Object public class A { // Class Body } public class A extends java.lang.Object { } Person.java, Student.java, examples from book
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Ramesh Yerraballi 4-4 Creating new classes The copy-and-paste problem:  Say,  we want to create  Student  who is  a copy of  Person , what happens if  we simply copy-and-paste the body  of  Person  to  Student  and change  names of the constructors and then  add any new methods needed.   Downsides : 1. Copy Garbage Paste Garbage  2. Maintenance/Versioning 3. Unable to exploit similarities  between the copy ( Student and the copied ( Person ).   Inheritance  solves all three  problems by defining an IS-A  relationship between  Student  and  Person  and  allowing  the following  changes to  Student : Student  can add new fields  (e.g.,  gpa ) Student  can add new  methods (e.g.,  getGPA ) Student  can override  existing methods (e.g.,  toString ) Disallowing  these: Student  cannot remove 
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

Ch4-1 - EE322C DataStructures Chapter4:Inheritance...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online