12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Abstract Classes and Interfaces The objectives of this chapter are: To explore the concept of abstract classes To understand interfaces To understand the.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
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.
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,
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
UML Class Diagram: class Rectangle
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CC1007NI: Further Programming Week Dhruba Sen Module Leader (Islington College)
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
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,
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
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.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Inheritance and Access Control CS 162 (Summer 2009)
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
Object Oriented Programming
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 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.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CLASS DIAGRAMS A classy approach to objects. The Basic Class Diagram  Class Name  Attributes (- indicates private each would have properties or accessor/mutator.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Interfaces & Abstract Classes (For CS III AP) March 2014.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Web Design & Development Lecture 9
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
University of Central Florida COP 3330 Object Oriented Programming
One class is an extension of another.
C++ Classes & Object Oriented Programming
12 Data abstraction Packages and encapsulation
UML Class Diagram: class Rectangle
One class is an extension of another.
Class Inheritance (Cont.)
Interface.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Inheritance Inheritance is a fundamental Object Oriented concept
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
Review of Previous Lesson
CSG2H3 Object Oriented Programming
Presentation transcript:

12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming

12-CRS-0106 REVISED 8 FEB 2013 Abstract

12-CRS-0106 REVISED 8 FEB 2013 Method with no implementation or specific behavior Handed over to the child class to implement its own –Parent class only declare the name of method Child class must implement or specify the method –Free to specify Use keyword “extends” Abstract Method

12-CRS-0106 REVISED 8 FEB 2013 A class with at least one abstract method must be declared abstract class An abstract class cannot be instantiated –As the instance must have already implement the abstract methods Abstract class will ensure the child implements the declared method Abstract Class

12-CRS-0106 REVISED 8 FEB 2013 Class diagram of an abstract –Italic class name –Italic method name if abstract Child extends the abstract parent class –Child must implement all abstract method –Or child will be declared abstract too Abstract Class Diagram AbstractStyle + normalMethod() + abstractMethod() Painting + abstractMethod()

12-CRS-0106 REVISED 8 FEB 2013 Example public abstract class AbstractParent{ public String toString(){ return “this is class Parent”; } public abstract void abstractMethod(); } public class Child extends AbstractParent{ public void abstractMethod(){ // implementing abstract method }

12-CRS-0106 REVISED 8 FEB 2013 The implementation of some methods are too vary for each child No concrete/actual object for the parent class –No such object exists –Parent doesn’t need to be instantiated Make a condition that the child class will have to implement some specific methods When to use/create Abstract class

12-CRS-0106 REVISED 8 FEB 2013 Animal -Weight + move() + breath() Example: Animal Hierarchy Tiger + move() + roar() Snake + move() + molting() Both tigers, fish, and snakes are animals and they all can move, but their ‘moving’ behavior are really different Fish + move() We don’t need to specify the behavior in animal class, let the child classes define themselves another view: We make sure that every animal can move

12-CRS-0106 REVISED 8 FEB 2013 Interface

12-CRS-0106 REVISED 8 FEB 2013 A special type of class which define a set of method prototypes To deliver a rule or mechanism to it’s child (class that implements) An interface can only contain –Abstract method –Final attributes –No constructor Interface

12-CRS-0106 REVISED 8 FEB 2013 Use keyword “implements” A class –Can “extend” only one class i.e. only one superclass –Can “implements” multiple interfaces Interfaces don’t have instance fields –they are merely a set of methods that the object implementing the interface methods must define Interface

12-CRS-0106 REVISED 8 FEB 2013 Class diagram of an interface –Tag > When class A implements Interface I, the diagram was denoted as Interface Class Diagram > Cloneable Color = white +shootWeapon() Trooper + shootWeapon()

12-CRS-0106 REVISED 8 FEB 2013 Like an abstract class, interface cannot be instantiated A class that implements interface(s) must implements all abstract methods An abstract class can also implements interface(s) An abstract class that implements interface(s) may not implements all abstract methods Interface

12-CRS-0106 REVISED 8 FEB 2013 Practical use of interfaces enables the separation of the interface and implementation of an object –The interface is provided by the interface definition –The implementation is provided by the classes which implement the interface methods Interface

12-CRS-0106 REVISED 8 FEB 2013 Interface usually named “…… - able” (but not always) –Serializable –Runnable –Cloneable Meaning that the implementing class able to do anything as the interface told –Implement interface to be able of doing some predefined tasks Interface

12-CRS-0106 REVISED 8 FEB 2013 Implementing an interface allows a class to become more formal about the behavior it promises to provide Interfaces form a contract between the class and the outside world, –this contract is enforced at build time by the compiler. Interface

12-CRS-0106 REVISED 8 FEB 2013 Example Interface Instagramable{ public abstract void takePicture(); public abstract void setFilter(); public abstract void sharePhoto(); } public class Kitten implements Instagramable{ public void takePicture(){ // implementing abstract method } public void setFilter(){ // implementing abstract method } public void sharePhoto(){ // implementing abstract method }

12-CRS-0106 REVISED 8 FEB 2013 Define some rules or mechanisms Group classes with no inheritance relationship Create an API When to use/create Interface

12-CRS-0106 REVISED 8 FEB 2013 Animal -Weight -Height Example: Animal Hierarchy Mammal -Fur/hair -mammaryGland Birds -Wings -beak WhaleTigerEaglePenguin BatOstrich

12-CRS-0106 REVISED 8 FEB 2013 Animal Example: Animal Hierarchy MammalBirds WhaleTigerEaglePenguin whales and penguins are aquatic animals and can swim, but bats, tigers, eagles and ostriches can not Bat We also can not put penguins and whales have the same parent class as they both are different Ostrich we can not put a swimming behavior in mammal, bird or animal class as not all mammals, birds and animals can swim

12-CRS-0106 REVISED 8 FEB 2013 Animal Example: Animal Hierarchy MammalBirds WhaleTigerEaglePenguin Here we can create an Aquatic Animal Interface that defines that animals that implements this interface will be able to swim and dive Bat Then we can make the whales and penguins implement Aquatic interface behavior > Aquatic + swim() + dive() Ostrich

12-CRS-0106 REVISED 8 FEB 2013 Animal Example: Animal Hierarchy MammalBirds WhaleTigerEaglePenguin Another example, tigers, eagles and penguins are carnivorous, while the rest are not Bat Create a Carnivorous Interface, and make the carnivorous animals implement the behaviors > Aquatic + swim() + dive() Ostrich > Carnivorous + eatMeat()

12-CRS-0106 REVISED 8 FEB 2013 Animal Example: Animal Hierarchy MammalBirds WhaleTigerEaglePenguin One class can only have one parent One class may implement many interface Bat > Aquatic + swim() + dive() Ostrich > Carnivorous + eatMeat() > CanFly + fly()

12-CRS-0106 REVISED 8 FEB 2013 Goods - price : int + display() Example: Taxable Goods Food - calories Book - author Toys and Books are classified as taxable goods, while foods are not > Taxable - taxRate = calculateTax() Toy - minimumAge

12-CRS-0106 REVISED 8 FEB 2013 Question?

12-CRS-0106 REVISED 8 FEB 2013 THANK YOU Credits M usic : Yonezawa Madoka - Oui! Ai Kotoba (Instrumental)