CPS 001 27.1 Today’s topics Java Language Inheritance Upcoming Electric Circuits (not in text) Reading Great Ideas, Chapters 5.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Overview of Java (continue). Announcements You should have access to your repositories and HW0 If you have problems getting HW0, let me know If you’ve.
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.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Inheritance COMP53 Sept Inheritance Inheritance allows us to define new classes by extending existing classes. A child class inherits all members.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Week 9 Generalization, inheritance, class hierarchy and graphics.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Computer Science I Inheritance Professor Evan Korth New York University.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
CSSE501 Object-Oriented Development
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
OO (Object Oriented) Programming Chapter 21 IB103 Week 12.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Arranging the border values of methods. import java.awt.*; import java.applet.Applet; public class Applet2 extends Applet { public void paint(Graphics.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
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 10 Inheritance and Polymorphism
CS2110: SW Development Methods Inheritance in OO and in Java Part 2: Topics: Forms of inheritance Interfaces in Java.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Inheritance and Polymorphism Chapter 11 Spring 2007 CS 101 Aaron Bloomfield.
Object-Oriented Programming: Polymorphism Chapter 10.
A Introduction to Computing II Lecture 3: Interfaces and Inheritance Fall Session 2000.
Abstract Classes. Recall We have the ability to create hierarchies of classes. Good design practice suggests that we move common methods as high as possible.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Class Inheritance Part I
University of Central Florida COP 3330 Object Oriented Programming
Chapter 11 Object-Oriented Design
Testing Object-Oriented Software Concepts and Definitions
Week 4 Object-Oriented Programming (1): Inheritance
An Introduction to Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance and Polymorphism
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Java Programming, Second Edition
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Object-Oriented Programming
Presentation transcript:

CPS Today’s topics Java Language Inheritance Upcoming Electric Circuits (not in text) Reading Great Ideas, Chapters 5

CPS Inheritance in Everyday Life  Inheritance kind of ideas are not strange in today’s world  When you order a new car you:  Pick model o That implies certain standard features  Pick a color  Choose engine o Often have several choices  Choose other options o Often many packages to choose from  Whatever you choose, it will have lots in common with other cars  We would say these common things are inherited from the model  Many of the things you chose might have sub-options  Thus, there would be another level of inheritance

CPS Inheritance in O. O. Programming  Object Oriented Programming  Basic Ideas is Don’t Reinvent the Wheel!  Wherever possible, build on the work of others  Reuse as much as possible  Modify only where necessary  Delete where not appropriate  Vocabulary  Parent Class or Super-Class  Child Class or Sub-Class  Child inherits from parent class

CPS Inheritance in Graphics  Assume we want to write a simple drawing package  A basic feature might be a shape  What functions might be associated with shape ?  I.e., what does every shape have?  Also, what do most shapes have?  Every: Most: Location Area Size Fill/NoFill Color Fill Color Orientation

CPS Shape Subclasses  What are the obvious shapes?  Oval  Line  Triangle  Rectangle  Polygon  Pie  Arc  How do these mesh with some of the methods suggested? Location Area Size Fill/NoFill Color Fill Color Orientation

CPS Subclasses of subclasses  Which of our Specific shapes might have subclasses?  Ovalcircle  Line  Triangleequilateral-triangle  Rectanglesquare  Polygonpentagon, hexagon, …  Pie  Arc  Still leaves many things to deal with  Parallelogram  Arbitrary polygons  Various line shapes  Use composites…

CPS Inheritance in Java  We say a subclass extends a parent class  Remember: public class classname extends java.applet.Applet  This means our class is a subclass of java.applet.Applet  Some of the other classes we’ve used also invoked inheritance  Button inherits from Component  Label inherits from Component  All of these classes include the words extends Component  Both TextField and TextArea are subclasses of a class called a TextComponent which, in turn, is a Component  Look at  Shows the inheritance hierarchy

CPS Inheritance in Java  Let’s think of our graphic/drawing problem  If we had a class shape, then we would write public class oval extends shape  In order to implement our oval class and public class circle extends oval  To make circle a subclass (special case of) oval  So, if the class shape had the method setColor, then it  could be used by oval and  could also be used by circle

CPS Inheritance in Java  Sometimes, this inheritance doesn’t work quite that easily  Take a method such as getArea which might be considered for the class shape  It is unlikely that one could come up with an area calculation that works for all shapes  This means we need to write specialized versions of getArea for each shape  This is called overriding a method  We simply write a new getArea in the subclass  On the the other hand, getArea for oval will work for circle  We might still override for efficiency reasons. Formula for circle is simpler.

CPS Access Control  Methods and data may have restricted access  Use public, private, or protected to specify  For methods :  public means: anyone can use  private means: can only use within class  protected means: only class and subclasses can use  For data fields:  public means: anyone can access or modify  private means: can only access or modify within class  protected means: only class and subclasses access or modify  Helps support Information Hiding

CPS Polymorphism  When you have inheritance hierarchy, can be more general  Just as we can say it’s a Ford, for all models and variations, we can say something is a shape and then allow ovals, circles, etc.  This ability to be more general is called polymorphism  In Java everything, by default, inherits from the class Object  Thus you have a hierarchy containing all classes  Object is at the “peak”