COMP 121 Week 03. Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided.

Slides:



Advertisements
Similar presentations
1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified.
Advertisements

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.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Reusable Classes.  Motivation: Write less code!
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Inheritance and Polymorphism.
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.
09 Inheritance. 2 Contents Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods.
Class Hierarchies. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An.
Inheritance Inheritance Reserved word protected Reserved word super
1. 2 Introduction to Inheritance  Access Modifiers  Methods in Subclasses  Method Overriding  Converting Class Types  Why up-cast?  Why down-cast?
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
19-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Introduction to Inheritance
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
LECTURE 07 Programming using C# Inheritance
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Intro to OOP with Java, C. Thomas Wu
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
What is inheritance? It is the ability to create a new class from an existing class.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
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.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
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.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
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.
Peyman Dodangeh Sharif University of Technology Fall 2014.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 12 Inheritance.
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
ATS Application Programming: Java Programming
ATS Application Programming: Java Programming
More inheritance, Abstract Classes and Interfaces
Advanced Java Programming
Inherited Classes in Java
Topics OOP Review Inheritance Review Abstract Classes
Inheritance and Polymorphism
Presentation transcript:

COMP 121 Week 03

Agenda Review this week’s expected outcomesReview this week’s expected outcomes Review Guided Learning Activity solutionsReview Guided Learning Activity solutions Introduce homework problemsIntroduce homework problems Question and answer sessionQuestion and answer session

Outcomes Explain the purpose, uses, and scope of inner classes.Explain the purpose, uses, and scope of inner classes. Describe inheritance and the Java mechanisms that implement it.Describe inheritance and the Java mechanisms that implement it. Distinguish between overloaded and overridden methods.Distinguish between overloaded and overridden methods. Organize a set of related classes into an inheritance hierarchy.Organize a set of related classes into an inheritance hierarchy. Determine when and how to call superclass methods from within a subclass.Determine when and how to call superclass methods from within a subclass. Apply the rules of implicit and explicit object conversion.Apply the rules of implicit and explicit object conversion.

Inner Classes Defined at same level as members and methodsDefined at same level as members and methods Are associated with a “parent” object of the outer class typeAre associated with a “parent” object of the outer class type Can access members of the “parent” objectCan access members of the “parent” object Each inner class can independently inherit from an implementation (“multiple implementation inheritance”)Each inner class can independently inherit from an implementation (“multiple implementation inheritance”)

Inner Classes

Guided Learning Activity Solutions for Week 03

Learning Activities Activity 3-1 Outcome: Explain the purpose, uses, and scope of inner classes.

Inheritance A mechanism for enhancing existing classesA mechanism for enhancing existing classes Base Class - the existing class that is used to define the new class (also called a superclass)Base Class - the existing class that is used to define the new class (also called a superclass) Derived Class - the new class defined using the base class (also called a subclass)Derived Class - the new class defined using the base class (also called a subclass) The subclass inherits behavior and state from the superclassThe subclass inherits behavior and state from the superclass Purpose: code reusePurpose: code reuse

Inheritance (cont’d) A base class and a derived class have an “is a” relationship (represented by a solid arrow with a “hollow triangle” tip that points to the superclass).A base class and a derived class have an “is a” relationship (represented by a solid arrow with a “hollow triangle” tip that points to the superclass). Animal Dog Retriever

Inheritance class Animal { public void eat() {…} } class Dog extends Animal { public void bark() {…} } class Retriever extends Dog {public void retrieve() {} } class Collie extends Dog { public void saveTimmy() {} } Animal is the base class for Dog.Animal is the base class for Dog. Dog is derived from Animal.Dog is derived from Animal. Retriever is derived from Dog.Retriever is derived from Dog. Dog is the base class for Retriever.Dog is the base class for Retriever. Dog and Animal are superclasses of Retriever and Collie.Dog and Animal are superclasses of Retriever and Collie. Dog, Collie, and Retriever are subclasses of Animal.Dog, Collie, and Retriever are subclasses of Animal.

Inheritance public class Animal { private String type; public Animal(String t) { type = t; } public String toString() { return “This is a “ + type; }}

Inheritance class Animal { void toString() { return “This is a “ + type; } } class Dog extends Animal { void toString() { return super.toString() + “\nIt’s “ + name + “ the “ + breed; } public static void main(String[] args) { Dog d = new Dog(“Rex”, “German Shepherd”); Dog d2 = new Dog(“Lassie”, “Collie”); System.out.println(d + “ “ + d2); }}

Learning Activities Activity 3-2 Outcome: Describe inheritance and the Java mechanisms that implement it. Activity 3-3 Outcome: Distinguish between overloaded and overridden methods.

Learning Activities public class A { void method1() {} void method1() {} void method3(String s) {} void method3(String s) {}} public class B extends A { void method1(int x) { /* Overload – parameters are different */ } void method1(int x) { /* Overload – parameters are different */ } void method3(String s) { /* Overrides A.method3 */ } void method3(String s) { /* Overrides A.method3 */ }} public class C extends A { void method1(){ /* Overrides A.method1 */ } void method1(){ /* Overrides A.method1 */ } void method2(int x, int y) { /* New method */ } void method2(int x, int y) { /* New method */ } void method2(int y, char c) { /* Overloads method2 */ } void method2(int y, char c) { /* Overloads method2 */ }}

Learning Activities public class D { void method3() {} void method3() {} void method1() {} void method1() {} void method2(int y, char c) {} void method2(int y, char c) {}} public class C { void method1() {} void method1() {} void method2(int x, int y) {} void method2(int x, int y) {} void method2(int y, char c) {} void method2(int y, char c) {}} public class B extends C { void method1(int x) { /* Overloads method1 */ } void method1(int x) { /* Overloads method1 */ } void method3(String s) { /* New method */ } void method3(String s) { /* New method */ }} public class A extends B { void method1() { /* Overrides C.method1 */ } void method1() { /* Overrides C.method1 */ } void method3(String s) { /* Overrides B.method3 */ } void method3(String s) { /* Overrides B.method3 */ }}

Learning Activities Activity 3-4 Outcome: Organize a set of related classes into an inheritance hierarchy.

Learning Activities public class Publication { private String isbn; private String isbn; private int state; // writing, editing, preproduction, done private int state; // writing, editing, preproduction, done private Date writingDeadline; private Date writingDeadline; private Date editingDeadline; private Date editingDeadline; private Date preproductionDeadline; private Date preproductionDeadline; private Date publicationDeadline; private Date publicationDeadline; public String getISBN() { return isbn; } public String getISBN() { return isbn; } public void setISBN(String isbn) { this.isbn = isbn; } public void setISBN(String isbn) { this.isbn = isbn; } // More get/set methods // More get/set methods}

Learning Activities public class Book extends Publication { private boolean isPaperback; private boolean isPaperback; private String author; private String author; private double royaltiesPaid; private double royaltiesPaid; // get/set methods for isPaperback, author, and royaltiesPaid // get/set methods for isPaperback, author, and royaltiesPaid} public class Magazine extends Publication { private String editor; private String editor; // get/setEditor // get/setEditor} public class Pamphlet extends Publication { // Nothing here // Nothing here}

Inheritance public class Dog extends Animal { private String name; private String breed; // Will this work? public Dog(String n, String b) { name = n; breed = b; } public Dog(String n, String b) { super(“Dog”); name = n; breed = b; } }

Inheritance class Animal { void display() { System.out.println(“This is a “ + type); } } class Dog extends Animal { void display() { super.display(); System.out.println(“\nIt’s “ + name + “ the “ + breed); } public static void main(String[] args) { Dog d = new Dog(“Rex”, “German Shepherd”); Dog d2 = new Dog(“Lassie”, “Collie”); d.display();d2.display(); }}

Inheritance and Casting class Dog {…} class Husky extends Dog {…} Dog d = new Husky(); // Legal – implicit cast Husky h = d; // Illegal – needs explicit cast Husky h = (Husky)d; // Legal Husky h = (Husky)new Dog(); // Throws exception

Learning Activities Activity 3-5 Outcome: Determine when and how to call superclass methods from within a subclass. Activity 3-6 Outcome: Apply the rules of implicit and explicit object conversion.

Learning Activities Polygon is the superclass, Triangle is the subclass Polygon p = new Triangle(); Triangle t = (Triangle)p; // cast required Triangle t = (Triangle)new Polygon(); // ClassCastException GraduateStudent is the subclass, Student is the superclass Student s = new GraduateStudent(); GraduateStudent gs = (GraduateStudent)s; // cast required Person is the superclass, Student is the subclass Person p = new Student(); Student s = (Student)p;

Learning Activities Vehicle is the superclass, Car is the subclass Vehicle v = new Car(); Car c = (Car)v; // cast required Vehicle is the superclass, Minivan is the subclass Vehicle v = new Minivan(); Minivan m = (Minivan)v; // cast required Vehicle is the superclass, Truck is the subclass Vehicle v = new Truck(); Truck t = (Truck)v; // cast required

Homework Assignments Due this weekDue this week Homework 1Homework 1 Due next weekDue next week Lab 1Lab 1 Homework 2Homework 2 Draft Reflection PaperDraft Reflection Paper

Question and Answer Session