Inheritance and Encapsulation Amit Gupta
Project 1 How did it go? What did you like about it? What did you not like? What can we do to help?
Suggestions Ask questions if you don t understand a concept Start Early!* (early!= (duedate 2)) Come to Office Hours Come to Consulting Hours Ask Questions!!! *This is the secret to success in this course!
Inheritance What do you know already?
Basic Inheritance Process of creating inherited class out of an existing class is called inheriting a class, deriving a class or subclassing. Existing class is called a parent class, base class or super class. New class is called child class, derived class or subclass. Child class inherits all of members and methods of parent class.
Why Inheritance? Adding Functionality Child class adds additional functionality to the parent class. No interference with basic functionality of the underlying class. For example, a simple Fish class might be able to do things like swim and feed. Another child class adds the ability to eat other Fish objects.
Why Inheritance? Code Reuse Minimizes any copying and pasting of code and promotes code reuse. Old mistakes are propagated with copying and pasting. Total amount of code increases even without the issue of errors introduced by copying and pasting.
Inheritance Skeleton public class derived extends base { } //Instance Vars for derived class //Omit I.V. s from base class //Overridden & New Methods //Omit Identical Methods from base
Inheritance in Java Base Class Fish Derived Classes Shark and Flounder
Object Class Do you know about Object class?
Object Class Every class in Java extends Object class. Every class in Java contains these methods. Java provides basic implementation of these methods in Object class but they need to be overridden so as to work with your object classes.
The super keyword Sometimes, we may wish to call methods defined in base class from the derived class. This is done with the keyword super. Example: super.baseclassmethod()
Constructors and Inheritance All the constructors in an object s inheritance tree must run when you make a new object. Child class object can be thought of containing parent class object inside of it. The call to super() must be the first statement in each constructor.
Overloaded Constructors Use this() to call a constructor from another overloaded constructor in the same class. Call to this() must be the first statement in a constructor. A constructor can have a call to super() OR this(), but never both.
Overriding methods and hiding members Methods in base class can be overridden in child classes to provide new functionality. Just as methods are overridden, member variables are hidden.
Representing different Polygons Using Inheritance Polygon class is the base class and represents a general polygon. Triangle and Rectangle are more specific polygons and hence they extend the general Polygon class by extending it. Inheritance hierarchy represents IS A relationship.
Polygon Class Represents general Polygon class. Number of things that can be done with general polygon are limited.
Triangle Class gettype() method allows us to perform specific to triangles. Super constructor must be the first line in the Triangle constructor.
Rectangle Class getarea() method is specific to a rectangle
Square Class Square is a more specific type of Rectangle. Square IS A rectangle.
Summarizing Inheritance Duplicate Code can be avoided. Common Code can be put in one place and if it needs to be changed, it has to be changed in only one place. Inheritance defines a common protocol for a set of classes related through inheritance.
Encapsulation and Data Hiding What do you already know? What is the purpose of encapsulation? What keywords pertaining to encapsulation are you familiar with?
Encapsulation and Data Hiding Encapsulation is the ability of an object to be a container for related properties i.e. data variables and methods. Data Hiding is the ability to shield variables from external access and is a consequence of encapsulation principle. Java provides different access levels for data hiding.
Package Access You have likely seen public, protected, and private access Package Access allows the class itself, as well as classes in the same package access Is this more or less strict than protected?
Encapsulation Summary Public Protected Package Private Class Itself Same Package Derived Class Other Classes Yes Yes Yes Yes Yes Yes Yes No Yes Yes No No Yes No No No Slide Author: Dr. Keith Frikken, Miami University
Questions
Quiz Time