Object-Orientated Design and Programming Unit 9: Abstract Classes and Interfaces Jin Sa.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming
OOP: Inheritance By: Lamiaa Said.
Abstract Classes and Interfaces The objectives of this chapter are: To explore the concept of abstract classes To understand interfaces To understand the.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes 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,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
UFCE3T-15-M Programming part 1 UFCE3T-15-M Object-oriented Design and Programming Block2: Inheritance, Polymorphism, Abstract Classes and Interfaces Jin.
1 Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L.
Object-Orientated Design and Programming Unit 8: Inheritance and Polymorphism Jin Sa.
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.
CC1007NI: Further Programming Week Dhruba Sen Module Leader (Islington College)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
Polymorphism & Interfaces
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
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.
What is inheritance? It is the ability to create a new class from an existing class.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Peyman Dodangeh Sharif University of Technology Fall 2014.
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. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CET203 SOFTWARE DEVELOPMENT Session 2B Constructors, Overriding and Overloading.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Object Oriented Programming
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
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.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
© 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.
1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
Testing in OO Environment The reasons for testing is not any different for any of the design and implementation methodologies, including OO methodology.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
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.
Advanced Programming in Java
Web Design & Development Lecture 9
Chapter 15 Abstract Classes and Interfaces
Object-Orientated Analysis, Design and Programming
Inheritance ITI1121 Nour El Kadri.
Chapter 11 Object-Oriented Design
Chapter 13 Abstract Classes and Interfaces
Advanced Programming Behnam Hatami Fall 2017.
Chapter 14 Abstract Classes and Interfaces
Final and Abstract Classes
Presentation transcript:

Object-Orientated Design and Programming Unit 9: Abstract Classes and Interfaces Jin Sa

Objectives The objectives are to know when to use abstract classes and when to use interfaces; to know how to define and use abstract classes and interfaces.

Abstract and Concrete Classes Concrete class can have (direct) instances. Abstract class cannot have its own (direct) instances. Abstract classes exist purely to generalize common behaviour that would otherwise be duplicated across (sub)classes. Abstract classes (normally) have abstract methods

Example of Abstract and Concrete classes

The Employee class contains one abstract method called calculatePay(). It is an abstract method with no implementation. each of the subclasses provides its own implementation of the calculatePay method. This means that each class provides an implementation of the abstract behaviour but each subclass does that in a slightly different way.

Abstract classes Class must be declared as abstract. Typically an abstract class contains one or more abstract method. Use abstract classes when you need to provide some functionality but also wish to defer some implementation to the subclasses. In UML it is shown in italics.

Inheritance Mechanisms Inheritance of Implementation. The operation signature and implementation method are passed without modification from superclass to subclass. Inheritance of Interface. The superclass offers an abstract operation signature. The subclass inherits the operation signature but must provide an implementation method if the class is to be concrete, i.e. have instances. Overriding Inheritance. The operation signature and method are inherited by the subclass but the method is modified to exhibit behaviour appropriate to that class.

Abstract classes in Java Consider an application that deals with different shapes –Circle, Triangle, Square –Common attributes and methods: colour, findArea() –How to define findArea() for the Shape class? Java provides a mechanism called abstract method to allow us to represent such methods. Abstract methods do not have any definition.

public abstract class Shape { private String colour="red"; Shape() { System.out.println("Shape's constructor called"); } … …//other methods public String toString(){ return "Colour is:"+ colour; } public abstract double findArea(); }

Abstract classes and methods Abstract methods such as findArea() provide “place holders” for methods that can sensibly be mentioned at one level, but that are going to be implemented in a variety of ways in the subclasses. An abstract class is a class usually with at least one abstract method. Abstract classes cannot have instances.

Extending an abstract class public class Square extends Shape{ private double length; /** Creates a new instance of Square */ public Square(double l) { length=l; } public double findArea(){ return length*length; }

Student activity 9.1 (D.1) Let’s model the concept of a pet. A pet has a name; it has activities. For example, a fish swims; a cat catches mice and a dog guards home. Define a collection of classes that would model the concepts of pet, fish, cat and dog. Provide suitable methods to display the information about a pet, including its name and the kind of activity it can do. Define a testing class in which you create some instances of Fish, Cat and Dog and display the information of these pets.

Polymorphism via abstract classes Student activity 9.2 Modify the above program. Create a collection of pets. Provide a polymorphic solution to display the information of each pet.

Interface An interface is an elegant way of defining the public services that a class will provide without being bogged down by implementation details. Think of an interface as a business contract between two parties. The class implementing the interface agrees to provide the services defined in that interface to other classes. Interface has no attribute

Interface in Java An interface is a class like construct that contains only constants and abstract methods. An interface defines the specification of a set of constants and methods. The interface acts as a guarantee or a contract: any class that implements the interface is guaranteed to provide implementations for these abstract methods. an interface is similar to an abstract class, but an abstract class can contain variables and concrete methods as well as constants and abstract methods.

Interface example public interface LifeStyle{ void haveGoodFood(); void haveAccommodation(); }

Implementing interface public class MScStudent2 extends Student implements LifeStyle{ private String supervisor; MScStudent2(String nm){ super(nm);} … … //other methods public void haveGoodFood(){ System.out.println("Lots of bread, meat and veg"); } public void haveAccommodation(){ System.out.println("have a single room in a shared house"); }

Polymorphism via interface Same as polymorphism via inheritance, Java also supports polymorphism via interfaces. public interface Speaker { void speak(); void announce(String s); } Speaker guest; guest=new Dog(); guest.speak(); guest=new Politician(); guest.speak();

Student activity 9.4 (D.2) Implementing polymorphism via interface Provide suitable definitions for the Dog class and the Politician class. Both the Dog class and the Politician class should implement the Speaker interface. You may also want to define other methods that are suitable for each class. For example, you can define a method called lobbying() in the Politician class.

Abstract classes vs Interfaces In an interface, the data must be constants; an abstract class can have all types of data. Each method in an interface has only a signature without implementation; an abstract class can have concrete methods.

When do we use an interface and abstract class? A strong is-a relationship that clearly describes a parent- child relationship should be modeled using classes. A weak is-a relationship, also known as an is-kind-of relationship, indicates that an object possesses a certain property. A weak is-a relationship can be modeled using interfaces. Interfaces can partially support multiple inheritances. The concepts of abstract classes and interfaces play an important role in software development. Using abstract classes and interfaces, to identify the boundaries of different parts of a software system without having to provide details about their implementations.