Abstract Classes and Interfaces Week 17.  Computer simulations  Abstract methods  Abstract classes  Interfaces  Multiple inheritance Abstract Classes.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Abstract Class and Interface
Interfaces.
Further abstraction techniques Abstract classes and interfaces 5.0.
Inheritance Inheritance Reserved word protected Reserved word super
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Further abstraction techniques Abstract classes and interfaces 1.0.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
©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.
Abstract Classes and Interfaces
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)
CC1007NI: Further Programming Week 3 Dhruba Sen Module Leader (Islington College)
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
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,
CISC6795: Spring Object-Oriented Programming: Polymorphism.
OOP (Java): Abstract/ OOP Objectives – –use a foxes-and-rabbits simulation to introduce abstract classes, interfaces, and multiple.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Lecture 9 Polymorphism Richard Gesick.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
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.
Further abstraction techniques
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
1 COS 260 DAY 21 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz corrected –Good results 9 Th Mini Quiz Today –40 min covering Chap 9 Assignment 5 Due.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
1 COS 260 DAY 22 Tony Gauvin. 2 Agenda Questions? 9 th Mini Quiz corrected –Good results Assignment 5 Not corrected yet Assignment 6 Posted (one more)
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.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Further abstraction techniques Abstract classes and interfaces 6.0.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
11 Further abstraction techniques
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Sections Inheritance and Abstract Classes
Inheritance and Polymorphism
Interfaces.
Interface.
Packages and Interfaces
Interfaces.
COS 260 DAY 19 Tony Gauvin.
Java Inheritance.
COS 260 DAY 23 Tony Gauvin.
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
COS 260 DAY 23 Tony Gauvin.
AP Computer Science DYRT Quiz
Further abstraction techniques
Inheritance Lakshmish Ramaswamy.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Abstract Classes and Interfaces Week 17

 Computer simulations  Abstract methods  Abstract classes  Interfaces  Multiple inheritance Abstract Classes and Interfaces CONCEPTS COVERED THIS WEEK

Simulations Programs are regularly used to simulate real-world activities They are often only partial simulations They often involve simplifications. –Greater detail has the potential to provide greater accuracy –Greater detail typically requires more resources

Benefits of simulations Support useful prediction. –The weather. Allow experimentation. –Safer, cheaper, quicker. Example: –‘How will the wildlife be affected if we cut a highway through the middle of this national park?’

Predator-prey simulations There is often a delicate balance between species. –A lot of prey means a lot of food. –A lot of food encourages higher predator numbers. –More predators eat more prey. –Less prey means less food. –Less food means...

The foxes-and-rabbits project

Main classes of interest Fox –Simple model of a type of predator. Rabbit –Simple model of a type of prey. Simulator –Manages the overall simulation task. –Holds a collection of foxes and rabbits.

Example of the visualization

The Animal superclass Fox and Rabbit have a superclass called Animal, which contains common fields such as alive and location Fox and Rabbit have different versions of a method called act, which models their behaviour at each time step

The act method of Animal Static type checking requires an act method in Animal Define act as abstract: abstract public void act(List newAnimals);

The Animal class public abstract class Animal { fields omitted /** * Make this animal act - that is: make it do * whatever it wants/needs to do. */ abstract public void act(List newAnimals); other methods omitted }

Abstract classes and methods Abstract methods have abstract in the signature. Abstract methods have no body. The presence of at least one abstract method makes the class abstract. Abstract classes cannot be instantiated. Concrete (i.e. non-abstract) subclasses complete the implementation.

Further abstraction

Selective drawing (multiple inheritance)

Multiple inheritance Having a class inherit directly from multiple ancestors. Each language has its own rules. –How to resolve competing definitions? Java forbids it for classes. Java permits it for interfaces. –No competing implementation.

Interface interface –A Java programming language keyword used to define a collection of method definitions and constant values. It can later be implemented by classes by means of the "implements" keyword.

Interfaces as method specifications Interfaces specify method signatures only Each method signature is followed by a semi-colon (;)

An Actor interface public interface Actor { fields omitted /** * Perform the actor's daily behavior. */ void act(List newActors); other methods omitted }

Features of interfaces All methods are abstract. There are no constructors. All methods are public. All fields are public, static and final.

Multiple interfaces Because interfaces simply specify method signatures, a single class can implement several different interfaces in order to ensure more methods can be implemented.

Classes implement an interface public class Fox extends Animal implements Drawable {... } public class Hunter implements Actor, Drawable {... }

Implementing an Interface When a class implements an interface, it is essentially signing a contract. –Either the class must implement all the methods declared in the interface and its superinterfaces, or the class must be declared abstract. –The method signature in the class must match the method signature as it appears in the interface. A class that implements the ActionListener interface must contain the method actionPerformed

Interfaces as types Implementing classes do not inherit code, but implementing classes are subtypes of the interface type. So, polymorphism is available with interfaces as well as classes.

Interfaces are useful for the following: Capturing similarities among unrelated classes without artificially forcing a class relationship Declaring methods that one or more classes are expected to implement Modelling multiple inheritance, a feature of some object-oriented languages that allows a class to have more than one superclass