{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

11-inheritance-polymorphism

11-inheritance-polymorphism - CSE 114 Computer Science I...

Info icon This preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 114 – Computer Science I Inheritance, Polymorphism, and UML Yosemite National Park, California
Image of page 1

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

View Full Document Right Arrow Icon
Containment A class contains another class if it instantiates an object of that class – “HAS-A” LinkedList HAS-A ListNode CAR HAS-A Engine
Image of page 2
Cats and Dogs • Cat Is a mammal Has fur Common house pet Has several breeds – Meows Scratches the furniture Catches mice • Dog – Is a mammal – Has fur – Common house pet – Has several breeds – Barks – Catches Frisbees – Chases squirrels – Can guard a house
Image of page 3

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

View Full Document Right Arrow Icon
Cats and Dogs Some classes share properties that are similar For example, cats and dogs are both mammals and common house pets It doesn’t make sense to duplicate shared properties unnecessarily Instead, we can abstract out common characteristics
Image of page 4
Cats and Dogs
Image of page 5

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

View Full Document Right Arrow Icon
6 Inheritance Inheritance A mechanism by which one class acquires (inherits) the properties (both data fields and methods) of another class. Superclass The class being inherited from Derived class The class that inherits The derived class is specialized by adding properties specific to it
Image of page 6
Inheritance An object class that is derived from another class inherits its instance variables and methods. child class -----> parent class “IS-A” subclass class derived class base class Any instance variable or method that is inherited by a subclass does not need to be redefined in the subclass.
Image of page 7

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

View Full Document Right Arrow Icon
Why use Inheritance? Customize classes (from the JDK or your own) • Benefits: – Don’t have to re-write code Abstraction – the JDK has many classes to customize, especially for GUIs Use methods and variables of fully tested classes Code in super classes can be used by a limitless number of subclasses – Making changes to common properties is easier – just change the parent class
Image of page 8
Inheritance Syntax public class ChildClass extends ParentClass { // instance variables for Child only // methods for Child only } ChildClass now contains all instance variables and methods defined above, as well as those defined inside ParentClass
Image of page 9

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

View Full Document Right Arrow Icon
Sample Inheritance Code public class Mammal { boolean hasFur = true; public void makeSound(){} } public class Cat extends Mammal { boolean hasClaws; String furColor; public void makeSound() { System.out.println(“Meow!”); } }
Image of page 10
Sample Inheritance Code II public class Persian extends Cat { String furColor = “White”; public void makeSound() { System.out.println(“Hisss!”); } } The Persian class inherits hasClaws from the Cat parent class. It overrides the makeSound() method, and hides the furColor field from Cat .
Image of page 11

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

View Full Document Right Arrow Icon
How to organize classes using Inheritance Determine what data you need to store. For example, for storing student and employee data: – Student data: name, age, GPA – Employee data: name, age, salary Divide up your classes according to state – since Students and Employees store different data, use separate classes Pool common data into a common parent class – Person data: name, age Have Student and Employee classes customize Person
Image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern