CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 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

Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Chapter 10: Introduction to Inheritance
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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:
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
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 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
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:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
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.
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.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
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
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Peyman Dodangeh Sharif University of Technology Fall 2014.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
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.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Inheritance Fundamentals. Inheritance A derived class extends a base class. It inherits all of its methods (behaviors) and attributes (data) and it may.
Inheritance 2 Mehdi Einali Advanced Programming in Java 1.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
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.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Advanced Programming in Java
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.
Topics OOP Review Inheritance Review Abstract Classes
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall

Agenda Inheritance – our last relationship! Primitives & control structures

Example

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, OR –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 Dog 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; }