Inheritance Notes: Using Object Oriented Design

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
CPSC150 Abstract Classes and Interfaces Chapter 10.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Abstract Classes and Interfaces
Computer Science I Inheritance Professor Evan Korth New York University.
Multiple Choice Solutions True/False a c b e d   T F.
Abstract classes and Interfaces. Abstract classes.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Intro to OOP with Java, C. Thomas Wu
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
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 and Access Control CS 162 (Summer 2009)
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
1 More About Derived Classes and Inheritance Chapter 9.
Advanced Programming in Java
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Lecture 10 Review of classes
Agenda Warmup AP Exam Review: Litvin A2
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Can perform actions and provide communication
Can perform actions and provide communication
MSIS 670 Object-Oriented Software Engineering
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
METHOD OVERRIDING in JAVA
Can perform actions and provide communication
Java Inheritance.
Chapter 10: Method Overriding and method Overloading
Object-Oriented Programming
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
Review of Previous Lesson
Review of Previous Lesson
Method Overriding and method Overloading
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Inheritance Notes: Using Object Oriented Design When Classes start a family…

HAS-A vs. IS-A Relationships Average Free Response Question The StudentRecord Class HAS-A array Money class Money object HAS-A couple of ints

HAS-A vs. IS-A Relationships New: IS-A Relationship Car IS-A Vehicle Triangle IS-A Polygon ProperFraction IS-A Fraction Many more to come… Car Vehicle Truck Corvette

Inheritance vocabulary Give an example of a polygon: Parent class – child class – grandchild class Super class – class - subclass Polygon Quadrilateral Triangle Rectangle

The Daddy of them all: Object All classes have the IS-A relationship with Object Inherited methods: toString() equals() hashCode() Many more that are not in the AP Subset Fraction Object Money ProperFraction

Creating a child class: extends public class Car extends Vehicle Car is the subclass It inherits all of Vehicle’s methods and variables It can “overwrite” the methods by using the same signature Ex: speedUp() method Car Vehicle Truck Corvette

A new keyword: super

Call Daddy’s methods: super Create a new Vehicle: Vehicle v1 = new Vehicle(); Create a new Car: Car c1 = new Car(); Polymorphism: If Vehicle and Car both have a speedUp() method, how do I call Vehicle’s speedUp () in Car’s methods? super.speedUp() Car Vehicle Truck Corvette speedUp() speedUp()

Call Daddy’s methods: super public class Vehicle{ private int speed; public void speedUp(){ speed++; S.O.P(“I’m fast”); } What does this do? In the runner: c1.speedUp(); public class Car extends Vehicle{ public void speedUp(){ S.O.P(“I’m really fast!”); super.speedUp(); } Car Vehicle Truck Corvette

Call Daddy’s constructor: super How does Car call Vehicle’s constructor? This is confusing because Vehicle’s constructor method is actually named _________________. super(parameter1, parameter2, …) Vehicle() Car Vehicle Truck Corvette

Never create me: abstract class abstract public class Polygon An object of the Vehicle class can never be instantiated (created) However, Vehicle can have child classes that CAN be instantiated Polygon Rectangle Triangle Square

Please create me: abstract method abstract double getArea(); An abstract method forces the creator of a child class to write the body of the method before the class can be compiled. Polygon Rectangle Triangle Square

Rules about constructors The superclass constructor can be called with the line super(param1, …) Daddy comes FIRST!!! The super constructor call MUST be the first line in the child constructor Car Vehicle Truck Corvette Car c1 = new Car();

More rules about constructors EMPHASIZE THIS! The magic of Java constructors!!! Daddy, help me!!!! Even if the word super is not present on the first line of the constructor, the child class constructor automatically calls its parent constructor before it does anything else. See sample questions…

Sample Question Answer? class Morf { private int numMorfs; public Morf(int m) {numMorfs = m;} public int getMorfs() {return numMorfs;} } class Dorf extends Morf private int numDorfs; public Dorf(int m, int d) {super(m); numDorfs = d;} public int getDorfs() {return numDorfs;} What will display when the following lines execute in a client file? Dorf dorf = new Dorf(10, 25); System.out.println("Morf count: " + dorf.getMorfs()); System.out.println("Dorf count: " + dorf.getDorfs()); Answer? See files MorfDorfQuestion.java and MorfDorfQuestion2.java

Another Sample Question class Morf { private int numMorfs; public Morf(int m) {numMorfs = m;} public int getMorfs() {return numMorfs}; public String toString(){ return “Morfs: “ + numMorfs;} } class Dorf extends Morf private int numDorfs; public Dorf(int m, int d) {super(m); numDorfs = d;} public int getDorfs() {return numDorfs;} What will display when the following lines execute in a client file? Dorf dorf = new Dorf(10, 25); System.out.println(dorf.toString()); System.out.println(dorf); Answer? See files MorfDorfQuestion.java and MorfDorfQuestion2.java

Don’t touch me: private variables private int myNum Private variables are only available to the class itself. They are NOT accessible to subclasses or client files. How are parent class private variables accessed?

Don’t touch me: private methods private void simplify() private methods can only be called within the class, not by children or clients.

Only family can touch me: protected methods protected int reproduce() Like private-Lite Only the class and child classes can call protected methods

Finally Let’s look at all of these topics in the following three files: Polygons.java PolygonsRunner.java Triangle.java Copy the files, paste them to your APCode folder, and add them to your workspace for a day of fun.

Question 1 Explanations: Answer? a.) didn’t implement moveBy method public abstract class Point {      private int x, y;      public Point(int x, int y) { this.x = x; this.y = y; }      public int getX() { return x; }      public int getY() { return y; }      public String toString() { return "(" + getX + "," + getY() + ")"; }      public abstract Point moveBy(int dx, int dy);      public abstract double distanceFrom(Point p); } public abstract class SpecialPoint extends Point {      public SpecialPoint(int x, int y) { < Code not shown > }      public double distanceFrom(Point p) { < Code not shown > } } public class SpecialerPoint extends SpecialPoint {      < Code not shown > } Which of the following is the minimal set of public constructors and/or methods required in the SpecialerPoint class, so that the statements SpecialerPoint pos = new SpecialerPoint(0, 0); System.out.println(pos.moveBy(10, 10).distanceFrom(pos)); compile with no errors?  a. public Point moveBy(int dx, int dy) b. public SpecialerPoint(int x, int y) public Point moveBy(int dx, int dy) c. public double distanceFrom(Point pos) d. public double distanceFrom(SpecialerPoint pos) public SpecialerPoint moveBy(int dx, int dy) e. public SpecialerPoint() public SpecialerPoint(int x, int y) Explanations: a.) didn’t implement moveBy method b.) implements moveBy method (other abstract method implemented in SpecialPoint) c.) no constructor d.) no constructor e.) could be correct —> not minimum set of constructors/methods Question from Susan Ke

Unused Slides

Neutered classes: final final public class Motorcycle By putting final in the class definition, it can never have subclasses Similarly, by putting final in front of a method name, its child classes can not overwrite the method