Inheritance and Encapsulation

Slides:



Advertisements
Similar presentations
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Advertisements

CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look 1 Xiang Lian The University of Texas – Pan American Edinburg,
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
Lecture 10: Inheritance Subclasses and superclasses The inheritance chain Access control The Object cosmic superclass The super keyword Overriding methods.
Inheritance using Java
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
The Java Inheritance Hierarchy CSIS 3701: Advanced Object Oriented Programming.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
CSC 142 Computer Science II Zhen Jiang West Chester University
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Exception Handling Part 2: Creating and Throwing Exceptions CSIS 3701: Advanced Object Oriented Programming.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Unit Testing CSIS 3701: Advanced Object Oriented Programming.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Static Data; More Inheritance reading:
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ) reading:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Inheritance and Polymorphism
Inheritance and Polymorphism CSIS 3701: Advanced Object Oriented Programming.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Inheritance. Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object Inheritance represents the IS-A.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Programming With Java ICS201 University Of Ha’il1 ICS 201 Introduction to Computer Science Inheritance.
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
Advanced Java Topics Chapter 9
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Inheritance and Polymorphism
Lecture 15: More Inheritance
Lecture 9-2: Interacting with the Superclass (super);
Lecture 10: Inheritance Subclasses and superclasses
Interfaces and Inheritance
CSC 205 Java Programming II
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Lecture 22 Inheritance Richard Gesick.
Polymorphism and access control
Week 6 Object-Oriented Programming (2): Polymorphism
Inherited Classes in Java
Encapsulation Inheritance PolyMorhpism
Object-Oriented Programming
CS18000: Problem Solving and Object-Oriented Programming
Lecture 14: Inheritance Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Building Java Programs
Building Java Programs
Polymorphism.
Lecture 15: Inheritance II
By Rajanikanth B OOP Concepts By Rajanikanth B
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
CSG2H3 Object Oriented Programming
Presentation transcript:

Inheritance and Encapsulation CSIS 3701: Advanced Object Oriented Programming

Inheritance and Encapsulation Key question: What components of superclass do subclass methods have access to? Can subclass methods access private superclass member variables? Subclass Superclass Superclass variables Superclass methods Additional variables Additional methods Superclass variables Superclass methods inherits

Inheritance and Encapsulation Subclass methods have no access to private components of superclass public class SecondClock extends Clock { … public String toString() { String result = ""; if (hour < 10) result += "0"; result += hour + ":"; if (minute < 10) result += "0"; result += minute + ":"; if (second < 10) result += "0"; result += second; return result; } Compiler error hour and minute are private to the Clock class

Inheritance and Encapsulation Why does a subclass not have access to its own variables? Key idea: developers of superclass and subclass may not be the same Clock int hour int minute SecondClock Code that directly refers to hour and minute Fred develops Clock Does not know that Barney has extended it Barney extends Clock

Inheritance and Encapsulation If developer of superclass changes internal representation, subclass will no longer work! Clock String hour String minute SecondClock Code that directly refers to hour and minute as integers Fred changes Clock Does not know that it will affect Barney’s class SecondClock no longer works

Public Access as Solution Solutions: Only access superclass variables using superclass public methods public String toString() { String result = ""; if (getHour() < 10) result += "0"; result += getHour() + ":"; if (getMinute() < 10) result += "0"; result += getMinute() + ":"; … Problem: What if no such public methods exist in superclass?

NameList Example Example: FlexName class extends NameList Override add for list full case: Increment maximum Rebuild names array to be one element longer Problem: these are private to NameList No public methods to allow access, either public class FlexName extends NameList { … public void add(String name) { if (isFull()) { maximum++; …

Protected Encapsulation Declare component as protected encapsulation May be accessed by subclasses but not other classes public class NameList { protected int maximum; protected int count; protected String[] names; … public class FlexName extends NameList { … public void add(String name) { if (isFull()) { maximum++; … Since FlexName extends NameList, this is now legal

Protected Encapsulation Problem: Developer of superclass may want to change internal representation in future No longer possible without affecting derived classes Better approach: Provide protected methods Other classes still have no access If internal representation changes, can just change method so behavior is still the same

Protected Methods public class NameList { private int maximum; … protected int getMaximum() {return maximum;} protected void setMaximum(int m) {maximum = m;} Other classes cannot use these public class FlexName extends NameList { … public void add(String name) { if (isFull()) { setMaximum(getMaximum() + 1); …