CPSC150 Interfaces Chapter 10.5-10.8. CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.

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

Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
ITEC200 – Week03 Inheritance and Class Hierarchies.
More about classes and objects Classes in Visual Basic.NET.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Inheritance Chapter 8.
CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CPSC150 Abstract Classes and Interfaces Chapter 10.
CPSC150 Abstract Classes Chapter 10. CPSC150 Directory Example (note: your assignment does not have all of this) DirectoryEntry name phone public void.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Abstract Classes and Interfaces
Abstract classes and Interfaces. Abstract classes.
COP 3003 Object-Oriented Programming - Polymorphism Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance in the Java programming language J. W. Rider.
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 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
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 Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
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 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
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.
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
Object-Oriented Programming: Polymorphism Chapter 10.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Programming in Java: lecture 7
Modern Programming Tools And Techniques-I
Chapter 10 Thinking in Objects
MSIS 670 Object-Oriented Software Engineering
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Advanced Java Programming
Advanced Programming in Java
Inheritance Inheritance is a fundamental Object Oriented concept
Java Programming, Second Edition
Inheritance.
Chapter 14 Abstract Classes and Interfaces
Presentation transcript:

CPSC150 Interfaces Chapter

CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class SubClass1 extends SuperClass first line of constructor is super( ); can use methods from SubClass1 or SuperClass private in parent is not accessible to children; protected is Subclasses can have only one parent

CPSC150 Inheritance Review Two reasons for inheritance 1.To use the methods of the parent 2.To use polymorphism (e.g. add Item to a library, but each Item is a CD or Video)

CPSC150 Abstract Classes Review Abstract methods enable polymorphism, but have no body Classes with abstract methods must be abstract Abstract classes can not have objects created from them Abstract classes can have useful concrete methods (e.g., setComment) and fields (e.g., Xposition)

CPSC150 Abstract Class Review Two reasons for Abstract Classes 1.Enables polymorphism when methods are not appropriate for superclass (e.g., draw) 2.Enforces a specification (in order to be a Shape, you must have a draw method)

CPSC150 One more detail: Interfaces Allow multiple inheritance –Be an animal AND black (other things are animals and other things are black) Can specify exactly what is needed for a concrete class –actionPerformed

CPSC150 Interfaces To implement in interface (parent), put: –public interface MyInterface instead of public class MyInterface in class using interface (child), put: –public class SubClass1 extends SuperClass implements MyInterface

CPSC150 Two arrows

CPSC150 Interfaces concept Just like a class, –but no variables (can have static final public fields) –no bodies for ANY method Like abstract class on speed Purpose? –polymorphism –specification

CPSC150 Interface Examples Comparator ActionListener

CPSC150 Abstract Classes vs Interfaces Use interfaces when possible because there can lots of classes following "implements" but only one "extends" Use interfaces if you do not want any fields or methods in your parent class Use abstract classes if you want subclasses to use common defined methods or fields

CPSC150 Problem Look at the code below. You have five types (classes or interfaces) U, G, B, Z, and X, and a variable of each of these types. What can you say about the relationships of the types? U u; G g; B b; Z z; X x; The following assignments are all legal: u = z; x = b; g = u; x = u; The following assignments are all illegal (they cause compiler errors): u = b; x = g; b = u; z = u; g = x;

CPSC150 Wrapper Classes, Many times an object is wanted instead of a primitive. Use a wrapper class: wraps the primitive around a java.lang wrapper class int i = 20; Integer iwrap = new Integer(i); myCollection.add(iwrap); // added as an Integer int iwrapvalue = iwrap.intValue( 37);