University of Central Florida COP 3330 Object Oriented Programming

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Chapter 10: Introduction to Inheritance
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
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.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
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.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Abstract Classes and Interfaces The Basics in Java.
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.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
CITA 342 Section 1 Object Oriented Programming (OOP)
Object Oriented programming Instructor: Dr. Essam H. Houssein.
© 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.
Object-Oriented Programming: Inheritance and Polymorphism.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Object-Oriented Programming: Polymorphism Chapter 10.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
 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.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Abstract classes and interfaces
Chapter 15 Abstract Classes and Interfaces
Advanced Programming in Java
Advanced Programming in Java
Sections Inheritance and Abstract Classes
Final and Abstract Classes
Inheritance and Polymorphism
One class is an extension of another.
Chapter 11 Object-Oriented Design
Object-Oriented Programming
Road Map Inheritance Class hierarchy Overriding methods Constructors
Chapter 13 Abstract Classes and Interfaces
Lecture 23 Polymorphism Richard Gesick.
One class is an extension of another.
Corresponds with Chapter 7
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Advanced Java Programming
Software Design Lecture : 12.
Inheritance Inheritance is a fundamental Object Oriented concept
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Java Programming, Second Edition
Java Inheritance.
Advanced Programming in Java
Object-Oriented Programming: Inheritance and Polymorphism
Fundaments of Game Design
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Final and Abstract Classes
Presentation transcript:

University of Central Florida COP 3330 Object Oriented Programming

Agenda Administrative Abstract Classes and Methods Final Classes and Methods Chapter 10 text

Abstract Classes and Methods

Abstract Classes Normally when creating classes in Object Oriented Programming the developer intends to create objects of that type Sometimes it is useful to declare classes that will never have objects created from them Classes that cannot have objects created from them are call abstract classes They are used only as superclasses in inheritance hierarchies and are referred to as abstract superclasses

Abstract Classes Abstract classes are incomplete Subclasses of the abstract class must declare the missing pieces to become concrete classes that object can be instantiated from Why? To provide an appropriate superclass from which other classes can inherit To share a common design

Abstract Classes How? An abstract class is a class that is declared abstract—it may or may not include abstract methods Abstract classes cannot be instantiated, but they can be subclassed An abstract method is a method that is declared without an implementation without braces followed by a semicolon

Abstract Classes How? When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class If it does not, then the subclass must also be declared abstract Class Members An abstract class may have static fields and static methods Can use these static members with a class reference as with any other class

Abstract Classes Abstract Class versus Interface Abstract classes are similar to interfaces Cannot instantiate them May contain a mix of methods declared with or without an implementation Abstract classes can Declare fields that are not static and final Define public, protected, and private concrete methods In interfaces all fields are automatically public, static, and final All methods that you declare or define (as default methods) are public Remember! We can extend only one class, whether or not it is abstract, whereas we can implement any number of interfaces

Abstract Classes Which should you use, abstract classes or interfaces? Consider using abstract classes if any of these statements apply to the development situation Want to share code among several closely related classes Expect that classes that extend the abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private) Want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong

Abstract Classes Which should you use, abstract classes or interfaces? Consider using interfaces if any of these statements apply to the development situation Expect that unrelated classes would implement the interface Want to specify the behavior of a particular data type, but not concerned about who implements its behavior Want to take advantage of multiple inheritance of type.

Abstract Classes An example of an abstract class in the JDK is AbstractMap, which is part of the Collections Framework. Its subclasses (which include HashMap, TreeMap, and ConcurrentHashMap) share many methods that AbstractMap defines get  put isEmpty,  containsKey  containsValue

Abstract Classes Code example In an object-oriented drawing application, you can draw circles rectangles lines Bezier curves and many other graphic objects These objects all have certain states and behaviors in common position orientation line color fill color moveTo() rotate() resize() draw()

Abstract Classes Some of these states and behaviors are the same for all graphic objects Others require different implementations All GraphObjects must be able to draw or resize themselves they just differ in how they do it This is a perfect situation for an abstract superclass Take advantage of the similarities and declare all the graphic objects to inherit from the same abstract parent object

Abstract Classes Code example

Abstract Classes With interfaces, a class that implements an interface must implement all of the interface's methods It is possible, however, to define a class that does not implement all of the interface's methods, provided that the class is declared to be abstract!

Final Classes and Methods

Final Classes Can declare some or all of a class's methods final. Use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses The Object class does this—a number of its methods are final, check the API! Make a method final if it has an implementation that should not be changed and it is critical to the consistent state of the object

Final Classes Methods called from constructors should generally be declared final If a constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results Can also declare an entire class final A class that is declared final cannot be subclassed This is particularly useful, for example, when creating an immutable class like the String class, check the API!