Lecture 3 Casting Abstract Classes and Methods Interfaces.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Object Oriented Programming
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.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Interfaces.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
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,
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
Java Interfaces Overview Java Interfaces: A Definition.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
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.
9/4/2015Abstract classes & Interface1 Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Polymorphism & Interfaces
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Internet Software Development Classes and Inheritance Paul J Krause.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
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.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
1 1 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code to respond.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Object Oriented Programming
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
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 Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
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.
Variations on Inheritance Object-Oriented Programming Spring
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
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.
Interfaces & Abstract Classes (For CS III AP) March 2014.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Chapter 15 Abstract Classes and Interfaces
Inheritance-Basics.
Final and Abstract Classes
Inheritance and Polymorphism
Java Programming Language
Interfaces.
Chapter 14 Abstract Classes and Interfaces
Interfaces.
Final and Abstract Classes
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Lecture 3 Casting Abstract Classes and Methods Interfaces

Casting Object Bicycle MountainBike RacingBike A class hierarchy Q: Mountainbike is a ? of bicycle? Q: what java keyword is used to do this hierarchy? Q:Does the Bicycle class use this keyword ?

Casting and object types When you create an object, its type is the class from which is was instantiated..e.g... myBike is a MountainBike (And it’s also is a Bicycle and an Object) –(Why an object?) But, a Bicycle may be a MountainBike but not necessarily… –(could be a RacingBike) MountainBike myBike = new MountainBike();

Casting allows the use of an object of one type in place of another type (down the hierarchy) Bicycle yourBike = new MountainBike(); myBike = (MountainBike) yourBike; **OK** Cast Object Bicycle MountainBike RacingBike Casting and object types

You can check the type of an object.. if (yourBike instanceof MountainBike) { // Cast MountainBike newBike = (MountainBike)yourBike; }

Casting Why are we looking at casting/object types? Because it’s used in GUI programming Used to check which GUI element was clicked etc… More later!

Abstract Class An abstract class represents a generic concept e.g. Shape, Food, Fruit, … it encapsulates the features that all kinds of elements of the concept have, e.g. Shapes have area, circumference these features are implemented as abstract methods e.g. area(), circumference() abstract methods cannot have an implementation meaningless at the conceptual, generic level

Abstract Classes An abstract class or method is defined by the keyword abstract –abstract class Shape { … abstract double area(); abstract double cicumference(); … }; Any class with an abstract method is automatically abstract and must be declared as such Note: no body

Abstract Class An abstract class cannot be instantiated A subclass of an abstract class can be instantiated only if it overrides each of the abstract methods of its superclass and provides an implementation for all of them –this subclass is known as a concrete class If a subclass of an abstract class does not implement all the abstract methods it inherits, the subclass is itself abstract (Note: static, final and private methods cannot be abstract as they cannot be overridden)

Abstract Class An abstract class effectively defines a complete programming interface – providing its subclasses with the method declarations for all of the methods necessary to implement that programming interface. –i.e. It defines what methods the subclass is going to have to implement… Note: an abstract method in Java is like a pure virtual function in C++

Abstract Class Example public abstract class Shape { public abstract double area(); // note no body } See CODE

Implementing the abstract class __-__________________________________________-__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ Public class Circle extends Shape { // put in attributes needed here... public Circle (double radius) { // constructor code.. } public double getRadius() { // code... } public double area() { //implement abstract method // Code for area calculation... } }

class Rectangle extends Shape{ // put in attributes needed here... // constructor public Rectangle (double width,double height) { // constructor code.. } public double area() { //implement area abstract method... } // other methods..etc } Implementing the abstract class

Using the Shape Objects Dynamic method lookup is used –the area of a circle is computed using the method defined by Circle class –the area of a rectangle is computed using the method defined by the Rectangle class Shape[] shapes = new Shape[3]; //setup array of shapes // then.. Instantiate the various shapes to be used..circle, square etc. // then.. Calculate the total area of the shapes.. double totalArea=0;// compute total area for (int i=0; i<shapes.length; i++){ totalArea+=shapes[i].area();

Interfaces in java

Interfaces Consider another type of shape, one where the centre point of the shape is known We want CenteredCircle to support area() and circumference() methods already defined without re-implementing the methods  Problem, no multiple inheritance in java abstract class CenteredShape { // instance fields double x; double y; } class CenteredCircle extends CenteredShape {…} etc…

Interfaces Solution  an interface An Interface is a simple a set of methods in java –Use the type Interface instead of class in the java source code… public interface myInterface { //method declarations... }

Interface An interface defines a protocol of behaviour that can be implemented by any class anywhere in the class hierarchy. It defines a set of methods but does not implement them. A class that implements the interface agrees to implement all the methods defined in the interface, thereby agreeing to certain behaviour.

Some Interface Rules an interface contains no implementation.. Just the method names/signatures the methods of an interface are implicitly abstract (but abstract qualifier not needed) the only fields allowed in an interface are constants (i.e. declared static final) all methods of an interface are implicitly public an interface cannot be instantiated  no constructor

Interface Example The interface Centered is defined as It defines the methods that a Shape subclass should implement if it knows the x,y coords of its center point public interface Centered { public void setCenter(double x,double y); public double getCenterX(); public double getCenterY(); }

Implementing an Interface A class uses the keyword implements to implement one or more interfaces public class className implements interfaceName { // class field definitions... // class method definitions... // interface method implementations... } public class class1 implements interface1,interface2{... }

Implementing an Interface public class CenteredCircle extends Circle implements Centered { // field defns double cx;//center x coord double cy; //center y coord // constructor public CenteredCircle(double cx, double cy, double radius){ super(radius); this.cx=cx; this.cy=cy; } Since the class CenteredCircle implements the circle interface, what extra methods must the CentredCircle class include?

Implementing an Interface // implementations for the interface methods public void setCenter(double x, double y){ cx=x; cy=y; } public double getCenterX(){ return cx; } public double getCenterY(){ return cy; }

Interfaces.. A bit more… Before writing a class definition, determine the public interface –the set of services offered to users of the class Java allows us to take this one stage further, by formally recording the interface as a Java interface a java interface is just a collection of abstract methods (i.e. we state the signatures, but not the bodies)

Using an Interface Where a class implements an interface instances of the class can be treated as objects of the interface type as well as objects of their own class type E.g. instances of CenteredCircle and CenteredRectangle can also be treated as - instances of Shape as they extend Shape - instances of Centered as they implement Centered

Quiz…what did you absorb? Explain in your own words what an interface is? Why is it needed in Java? Can an interface implement its methods? Can an abstract class? Can an interface be implemented by classes from different hierarchies? (e.g. a Bicycle class, and a Circle class? Can a class have more than one superclass? Can a class implement more than one interface? Stopped here

Abstract Classes vs Interfaces Differences between abstract classes and interfaces –An interface cannot have any implementation, whereas an abstract class can. –An interface is not part of the class hierarchy. Unrelated classes can implement the same interface. –A class can implement many interfaces but can have only one superclass.