Further Abstraction Techniques Chapter 10. Abstract Classes There are times when you do not want someone to be able to make an object of your class. For.

Slides:



Advertisements
Similar presentations
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Advertisements

24-Aug-14 Abstract Classes and Interfaces. Java is “safer” than Python Python is very dynamic—classes and methods can be added, modified, and deleted.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Abstract Class and Interface
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
ABSTRACT CLASSES AND INTERFACES. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
15-Jun-15 Abstract Classes and Interfaces. 2 Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Abstract Classes and Interfaces. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
©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.
Further abstraction techniques Abstract classes and interfaces 3.0.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
CC1007NI: Further Programming Week Dhruba Sen Module Leader (Islington College)
CC1007NI: Further Programming Week 3 Dhruba Sen Module Leader (Islington College)
Abstract classes and Interfaces. Abstract classes.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Dec Abstract Classes and Interfaces. Eclipse trick CTRL + D will remove lines Organization Bookmarks TODOs – marking something as //TODO allows.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
Inheritance (Part 4) Abstract Classes 1.  sometimes you will find that you want the API for a base class to have a method that the base class cannot.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Abstract Classes and Interfaces Week 17.  Computer simulations  Abstract methods  Abstract classes  Interfaces  Multiple inheritance Abstract Classes.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
When is a class not a class? When it is either abstract or an Interface.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Sixth Lecture ArrayList Abstract Class and Interface
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Inheritance and Polymorphism
Object Oriented Programming (OOP) LAB # 8
Interface.
Interfaces.
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
CSC 113: Computer programming II
COS 260 DAY 23 Tony Gauvin.
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
COS 260 DAY 23 Tony Gauvin.
Further abstraction techniques
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Topics OOP Review Inheritance Review Abstract Classes
Inheritance Lakshmish Ramaswamy.
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

Further Abstraction Techniques Chapter 10

Abstract Classes There are times when you do not want someone to be able to make an object of your class. For example, a class that is only supposed to be sub-classed, like the item from the last example. An abstract class is a class that cannot be created and can only be sub- classed.

Why? If you remember dynamic typing, we are allowed to use any object that matches the static type or any subclass of that type. That means we can write code that expects the base class and really have subclass object live there during execution. Then we can use method overriding to make those objects do what we want.

Abstract Method An abstract method is a method that contains only the signature and no method body. Ex –abstract public void act( Field currentField, Field updatedField, List newAnimals ); Notice the keyword abstract, that indicates that this method will have no body Any class that has at least one abstract method, must be declared as abstract.

Abstract Class Example public abstract class Animal { //fields omitted abstract public void act( Field currentField, Field updatedField, List newAnimals ); //other stuff left out } Again notice the keyword abstract

Sub-classing Abstract Classes When you sub-class an abstract class, if you intend to allow people to create objects of your class, you must override the abstract method(s) This makes sense if you think about it, because an abstract class is intended to be sub-classed because it doesn’t know how the sub-class will handle the abstract method So your subclass has to tell the computer what it means for that object to perform the method

Interfaces There is another type of class that is like abstract classes are interfaces An interface defines a type, just like classes do. They tell the computer what classes that implement the interface will do. They do not tell the computer how those classes will perform those tasks.

Example of Interface public interface SimulatorView { void setColor( Class cl, Color color ); boolean isVisible( Field field ); void showStatus( int step, Field field ); } Notice the keyword interface and complete lack of method bodies and access modifiers. All methods are assumed to be public.

Implementing an interface To implement an interface you need to let the computer know that you want to do so. You add some information to the class declaration public class AnimatedView extends JFrame implements SimulatorView { … }

Abstract Class vs. Interface It may seem that an abstract class and an interface are the same thing They are not An abstract class can have fields and other methods that have complete methods including bodies. Interface cannot have either of those.

Which to Choose So you might ask yourself, which should I choose for a class where I want to do this. The answer is it depends. If you can choose either an interface or an abstract class, then choose the interface Why? Because Java will allow you to implement more than one interface, but only extend one class.