Inheritance #1 First questions Similar to Python? What about visibility and encapsulation? – can an object of the child class access private members.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Final and Abstract Classes
Object Oriented Programming with Java
Python Objects and Classes
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Chapter 1 Inheritance University Of Ha’il.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Inheritance Inheritance allows the derivation of a new class from an existing one, for the purpose of reuse, enhancement, adaptation, etc. superclass (a.k.a.
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
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.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Inheritance using Java
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Intro to OOP with Java, C. Thomas Wu
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Overloading and Constructors
Class Inheritance (Cont.)
Interface.
Inherited Classes in Java
Object-Oriented Programming
More on Creating Classes
Lecture 8 Inheritance CS140 Dick Steflik.
Final and Abstract Classes
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
ITE “A” GROUP 2 ENCAPSULATION.
Presentation transcript:

Inheritance #1

First questions Similar to Python? What about visibility and encapsulation? – can an object of the child class access private members of its parent? Is the inheritance hierarchy reflected in type declaration? How does method overriding work?

Stepper in Python from Counter import Counter class Stepper (Counter): # from Python Review #1 def __init__(self): Counter.__init__(self) def reset(self): self.value = 0 # parent’s constructor called explicitly # where is my parent? # child method; others inherited

Testing Stepper # Testing the child class: s = Stepper() print "initial value", s.current() s.step() print "after one step", s.current() s.reset() print "after reset", s.current() # inherited # new method

Stepper in Java public class Stepper extends Counter { public Stepper () { super(); } public void reset() { this.value = 0; } // parent's constructor called implicitly // child method; others inherited

Recall Java’s Counter public class Counter { private int value; public Counter () { this.value = 0; } public void step() { this.value += 1; } public int current() { return this.value; } // encapsulation enforced // static typing

What is inherited? Methods are public – they are inherited Instance variable “value” is private – it is not inherited So how do we access “value” in Stepper?

Use protected visibility public class Counter { protected int value; public Counter () { this.value = 0; } public void step() { this.value += 1; } public int current() { return this.value; } // only “package” encapsulation enforced // child class has access

TestStepper in Java public class TestStepper { public static void main (String[] args) { // Testing the child class: Stepper s = new Stepper(); System.out.println("initial value " + s.current()); s.step(); System.out.println("after one step " + s.current()); s.reset(); System.out.println("after reset " + s.current()); } // inherited // new method

NO! Will this work? public class TestStepper { public static void main (String[] args) { // Testing the child class: Counter s = new Stepper(); System.out.println("initial value " + s.current()); s.step(); System.out.println("after one step " + s.current()); s.reset(); System.out.println("after reset " + s.current()); } // Stepper is a “kind of” Counter // no method reset() in class Counter!

Must use a cast public class TestStepper { public static void main (String[] args) { // Testing the child class: Counter s = new Stepper(); // Stepper is a “kind of” Counter System.out.println("initial value " + s.current()); s.step(); System.out.println("after one step " + s.current()); ((Stepper)s).reset(); System.out.println("after reset " + s.current()); } // tell javac “s” is actually a Stepper object

public class TestStepper { public static void main (String[] args) { // Testing the child class: Stepper s = new Counter(); System.out.println("initial value " + s.current()); s.step(); System.out.println("after one step " + s.current()); s.reset(); System.out.println("after reset " + s.current()); } Will this work? // Counter a “kind of” Stepper? NO!

Incrementer in Python # parent’s constructor called explicitly # child instance variable # overrides parent step() Recall this method overriding example: from Counter import Counter class Incrementer (Counter): # from Python Review #1 def __init__(self, increment=1): Counter.__init__(self) self.increment = increment def step(self): self.value += self.increment

Incrementer (child) 0 : 3 Incrementer (child) 1 : 6 Counter (parent) 0 : 1 Counter (parent) 1 : 2 Testing Incrementer # Testing the parent class: obj = Counter() for i in range(2): obj.step() print "Counter (parent)", i, ":", obj.current() # Testing the child class: obj = Incrementer(3) for i in range(2): obj.step() print "Incrementer (child)", i, ":", obj.current()

Incrementer in Java public class Incrementer extends Counter { private int increment; public Incrementer (int increment) { super(); this.increment = increment; } public Incrementer () { super(); this.increment = 1; } public void step() { this.value += this.increment; } // constructor for default increment // overrides parent step() // child instance variable // constructor for increment Overloaded!

public class TestIncrementer { public static void main (String[] args) { Counter obj; // Testing the parent class: obj = new Counter(); for (int i = 0; i < 2; i++) { obj.step(); System.out.println("Counter (parent) " + i + ":“ + obj.current()); } // Testing the child class: obj = new Incrementer(3); for (int i = 0; i < 2; i++) { obj.step(); System.out.println("Incrementer (child) " + i + ":“ + obj.current()); } Counter (parent) 0 : 1 Counter (parent) 1 : 2 TestIncrementer in Java P o l y m o r p h i s m ! Incrementer (child) 0 : 3 Incrementer (child) 1 : 6