CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739

Slides:



Advertisements
Similar presentations
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.
Advertisements

CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Chapter 10: Introduction to Inheritance
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE116: Introduction to Computer Science 2 Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Thanks, Andrew. Reminders: –wear the wireless mike –combination is 1-31 –speak slowly, pause for questions –I’d rather you get through less material than.
Peyman Dodangeh Sharif University of Technology Fall 2014.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Inheritance Notes Chapter 6 1. Inheritance  you know a lot about an object by knowing its class  for example what is a Komondor? 2
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
© 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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
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.
Sections Inheritance and Abstract Classes
Lecture 12 Inheritance.
Objects as a programming concept
One class is an extension of another.
Abstract Classes.
Advanced Programming Behnam Hatami Fall 2017.
Java Inheritance.
Abstract Classes and Interfaces
Topics OOP Review Inheritance Review Abstract Classes
Presentation transcript:

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:

Announcements Exam 3 on Wednesday 11/10 –covers material from last exam up to and including Friday 11/05 Exam review in lecture on Monday 11/08 –study over the weekend, and bring questions to class on Monday Sample questions on web-site over the weekend

Agenda Collections: quick review Inheritance – our last relationship! –we will only introduce inheritance today –we’ll continue after the exam

Collections: concepts interface: java.util.Collection –E is the type of element contained in the collection – replace by an actual type concrete implementations (examples): –java.util.HashSet at most one of each internal order irrelevant to client –java.util.ArrayList multiples permitted maintains order; client can determine order

Collections: usage - 1 To declare a variable of type HashSet of String: HashSet names; To create a HashSet of String object, and assign its reference to the variable declared above: names = new HashSet ();

Collections: usage - 2 To add a String to the HashSet: names.add(“Fred”); To remove a String from the HashSet: names.remove(“Fred”);

Collections: usage - 3 for-each loop – process each element of a collection of elements of type E: for (E item : collection) { // do something with item } Example: to print out each String in names: for (String name : names ) { System.out.println(name); } Declared as: HashSet Declared as: HashSet

Example from Monday

What’s wrong with this code? package noninheritance; public class Cat implements Noisy { private String _myName; public Cat(String n) { _myName = n; } public String getName() { return _myName; public String sound() { return "meow"; } package noninheritance; public class Dog implements Noisy { private String _myName; public Dog(String n) { _myName = n; } public String getName() { return _myName; public String sound() { return ”ruff"; }

Code duplication a “code smell” package noninheritance; public class Cat implements Noisy { private String _myName; public Cat(String n) { _myName = n; } public String getName() { return _myName; public String sound() { return "meow"; } package noninheritance; public class Dog implements Noisy { private String _myName; public Dog(String n) { _myName = n; } public String getName() { return _myName; public String sound() { return ”ruff"; }

Number one in the stink parade is duplicated code. If you see the same code structure in more than one place, you can be sure that your program will be better if you find a way to unify them. Refactoring: Improving the Design of Existing Code, Martin Fowler, page 76

Inheritance Inheritance is the last of the relationships we will study this semester. It is a relationship between: –two classes –two interfaces Inheritance is (syntactically) simple Inheritance is (conceptually) messy

original design

class to class inheritance I

In code: public abstract class Noisy {…} public class Cat extends Noisy {…}

Abstract class A class which mixes method specifications (abstract methods) with fully defined methods (concrete methods) is abstract. An interface contains only abstract methods (they are labelled ‘abstract’ implicitly).

Abstract class ‘abstract’ keyword in class header cannot be instantiated

Inheritance (“extends”) Source class: –subclass –child class –derived class Target class: –superclass –parent class –base class

Implications of “extends” Same type implications as for interfaces: –instance of subclass belongs to subclass type and superclass type inheritance: non-private members of superclass can be accessed via subclass object. –e.g. it’s as if methods of superclass were defined in subclass

[A] common duplication problem is when you have the same expression in two sibling subclasses. You can eliminate this duplication by using Extract Method (110) in both classes then Pull Up Method (322). Refactoring: Improving the Design of Existing Code, Martin Fowler, page 76

class to class inheritance IIa

Code duplication a “code smell” package noninheritance; public class Cat implements Noisy { private String _myName; public Cat(String n) { _myName = n; } public String getName() { return _myName; public String sound() { return "meow"; } package noninheritance; public class Dog implements Noisy { private String _myName; public Dog(String n) { _myName = n; } public String getName() { return _myName; public String sound() { return ”ruff"; }

Refactored code (-: a breath of fresh air :-) package inheritance; public class Cat extends Noisy { public Cat(String n) { super(n); public String sound() { return "meow"; } package inheritance; public class Cat extends Noisy { public Dog(String n) { super(n); public String sound() { return ”ruff"; } package inheritance; public abstract class Noisy { private String _myName; public Noisy(String name) { _myName = name; } public abstract String sound(); public String getName() { return _myName; }