Coming up A quick word about abstract How a class interfaces

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

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.
Inheritance Inheritance Reserved word protected Reserved word super
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Polymorphism and Inheritance. Overview  Understanding Inheritance  Designing an Inheritance Tree  What does inheritance really help with  Polymorphism.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Polymorphism & Interfaces
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
ECE 122 March 24. Motivation I am tasked to write two classes, one for cat, one for sheep. That is easy. I roll up my sleeve and get it done!
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
CS2110: SW Development Methods Inheritance in OO and in Java Part 2: Topics: Forms of inheritance Interfaces in Java.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
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.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
Object Oriented Programming
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
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.
CS 210 Introduction to OO Design August 24, 2006
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
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: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Coming up Inheritance – Code duplication – Super classes – Constructors Polymorphic collections – “Anywhere a super class is, a sub class can go” Casting.
Lecture 5:Interfaces and Abstract Classes
Lecture 12 Inheritance.
Abstract Classes and Interfaces
Abstract Classes and Interfaces
One class is an extension of another.
03/10/14 Inheritance-2.
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
One class is an extension of another.
More inheritance, Abstract Classes and Interfaces
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Inheritance, Superclasses, Subclasses
CS18000: Problem Solving and Object-Oriented Programming
Abstract Classes Page
Inheritance Inheritance is a fundamental Object Oriented concept
Overriding Methods & Class Hierarchies
More About Inheritance & Interfaces
Java Inheritance.
Interfaces and Abstract Classes
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
Fundaments of Game Design
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Final and Abstract Classes
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
Topics OOP Review Inheritance Review Abstract Classes
Extending Classes Through Inheritance
Presentation transcript:

Coming up A quick word about abstract How a class interfaces Making Pets The Deadly Diamond of Death Interfaces (not interfaces)

OO Lecture 15 Interface this

Coming up A quick word about abstract How a class interfaces Making Pets The Deadly Diamond of Death Interfaces (not interfaces)

A quick word about abstract OO A quick word about abstract You looked at abstract in the last lab A quick review Mark classes that you don’t want instantiated to be abstract. Subclasses can still be instantiated An abstract class has little to no use unless it is extended A non-abstract class, the ones we normally deal with are called concrete classes

Abstract methods OO You can also mark methods as abstract If you have a method that doesn’t make sense unless it is in a more specific subclass, then mark it abstract. A method like move() – can you define a general way for everything to move? An abstract class must be extended An abstact method must be overridden An abstract method has no body.

public abstract void move(); If you declare an abstract method... ... You must mark the class as abstract You cannot have an abstract method in a concrete class Buy you can mix abstract and non abstract methods in an abstract class No body!

If you do use abstract methods You have to implement any abstract methods in a superclass in the first concrete class(es) down the inheritance tree That means you must provide a method body in the first concrete class that inherits from the abstract class OO

OO So what is the point? It’s all about how your classes interact and what their interface is

Coming up A quick word about abstract How a class interfaces Making Pets The Deadly Diamond of Death Interfaces (not interfaces)

How a class interfaces OO We say a class’ interface is basically what public methods it provides. By using these public methods, an other class can interact with it A class’ public methods are a bit like buttons another class can press. We’ve shown this by using a remote control as a metaphor.

Code reuse Code reuse is a big thing in programming All/most of the Java Library code is very helpful and very useful It pays to make your classes reuseable.

The point By putting abstract methods into your superclasses, you’ve guaranteed that the subclasses will provide certain methods This makes big projects easier. You know exactly what methods you can definitely call from any subclasses of a common superclass It also makes reusing code easier too

End of inheritance That is the end of formal teaching of inheritance. Inheritance is BIG it is important Many programming languages make heavy use of it. But....it is hard to understand at first

Suggested Reading Pg 165 onwards in HeadFirst Java Don’t fear the oop (google for it) Java tutorials Chapter 8 in BlueJ

Coming up A quick word about abstract How a class interfaces Making Pets The Deadly Diamond of Death Interfaces (not interfaces)

A different way of guaranteeing behaviour

Do you remember? Remember this animal hierarchy? Animal Canine Dog Wolf Feline Cat Tiger Rodent Hamster

Re-using this code What if we wanted to reuse this code to model someone’s pets? We want to implement some new stroke() methods, feedTreat() methods and some play() methods. How should we do this?

Buzz Groups With 4 or so people around you: Come up with some places in the hierarchy that we could add those methods to the appropriate animals. Think about using abstract methods in some cases Come up with the good points and the drawbacks for each location Smart Alecs – don’t mention the i-word, you’ll ruin the surprise!

Where could the play() method go? Animal Canine Dog Wolf Feline Cat Tiger Rodent Hamster

Option 1 Put methods in Animal class Pro Con All animals inherit behaviour, no need to touch subclasses Con We don’t really want to play with a tiger or stroke a wolf, this could be a dangerous choice Animal Canine Dog Wolf Feline Cat Tiger Rodent Hamster

Option 2 Pro Put methods in Animal class but mark them as abstract Con All animals inherit behaviour, they implement their own version at the first concrete subclass Con You’d have to sit and implement behaviour in all those low level classes, even the ones you don’t want to have the methods in! Put methods in Animal class but mark them as abstract Animal Canine Dog Wolf Feline Cat Tiger Rodent Hamster

Option 3 Put methods in only the classes that need the behaviour Pro Only animals you want to have the methods will have them Con You’d have to agree an exact protocol for pet methods with anyone who will ever use your class You couldn’t call any of the pet methods if you were treating the object as type Animal – Animal doesn’t have those specific methods! Put methods in only the classes that need the behaviour Animal Canine Dog Wolf Feline Cat Tiger Rodent Hamster

Any other ideas? Make your suggestions What can we do to solve this?

New Pet Class It sounds like we need a separate super class, Pet: Pet Animal Canine Dog Wolf Feline Cat Tiger Rodent Hamster

Coming up A quick word about abstract How a class interfaces Making Pets The Deadly Diamond of Death Interfaces (not interfaces)

Multiple inheritance OO In Java, multiple inheritance is not allowed This is because of the deadly diamond of death! Class A Class B foo() Class C foo() If foo() is called on an object of type D, which method gets called? Class D

On a quest for a solution OO On a quest for a solution Lets think a bit more about class interfaces. The class interface is the public methods it provides Like the buttons on the remote

Buttons eat sleep bark Memory dog1 Dog

Dog has some inherited methods And some specific methods Now we want to add a method called Play roam toString play eat sleep bark

Coming up A quick word about abstract How a class interfaces Making Pets The Deadly Diamond of Death Interfaces (not interfaces)

OO Interfaces We can add obligations for specific methods with an Interface An Interface is a 100% abstract class Like a class But no implimentation

To define an interface public interface Pet{ public abstract void play(); }

To implement an interface public class Dog extends Canine implements Pet{ String name; //Dog code here public void play(){ System.out.println(“Dog is playing”); } //more Dog code here

Implementing an interface is like adding an unprogrammed button to the remote roam roam roam toString toString toString play play play eat eat eat sleep sleep sleep meow squeek bark Cat Hamster Dog

Programming the button By implementing the interface you are adding the unprogrammed button You have to program the button in the class that implements the interface Otherwise the compiler grumbles

And this is useful because...? Lots of reasons! You know that everything that implements an interface will have those methods that you call They don’t even need to be from the same inheritance tree – lets say you had a recyclable interface, you could have an inheritance tree of metals and another of plastics, but some types of both could be recyclable If someone wants to add a class to your program, you don’t need to give them the superclass, just make them implement the common interface and the class will fit in just fine

Also... Classes can extend only one class But they can implement many interfaces So Dog can be a Pet And it can be Comparable (allows collections to sort objects) And it can be Serializable (allows Java to ‘deflate’ the object to save it for when you next run the program) (Useful for saving your leveled up Dark Mystical Elf character ;) )

Summary A quick word about abstract How a class interfaces Making Pets The Deadly Diamond of Death Interfaces (not interfaces)