1 Object-Oriented Software Engineering CS288. 2 Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
Computer Science and Engineering College of Engineering The Ohio State University Lot More Inheritance and Intro to Design Patterns Lecture 12.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Lecture 8: Object-Oriented Design. 8-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Good object-oriented programming is really.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
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.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance ndex.html ndex.htmland “Java.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
 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 12 Inheritance.
Inheritance and Polymorphism
Java Inheritance.
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Overloading and Constructors
Chapter 8 Class Inheritance and Interfaces
Topics OOP Review Inheritance Review Abstract Classes
Presentation transcript:

1 Object-Oriented Software Engineering CS288

2 Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with other software Should be able to write own code if you know what other code computes, but without knowing how other people’s code is written (in a perfect world) Presentation relies on material from

3 Interfaces in Java Interface is a reference type, –can’t have run time objects of that type until some other class implements the interface –can have method argument of that type –can declare objects of that type Can contain only –constants, –method signatures, –and nested types There are no method bodies or instance fields Can only be implemented by classes or extended by other interfaces (Extensions are later in this presentation)

4 Interfaces in Java, example interface package building; import java.util.Vector; public interface Building_interface { void addRoom(Room newRoom); void setRoom(Vector newRooms); Vector getRooms(); String[ ][ ] roomsToArrayArray(); } Specify method name method arguments return type Note, still have same basic Java sytax for importing other classes, packages etc

5 Interfaces in Java, example interface package building; import java.util.Vector; public class Building implements Building_interface { /* code goes here for all methods listed in Building_interface */ /* class may also contain other methods not in interface */ }

6 Interfaces in Java, 2 nd example interface package building; import java.util.Vector; public interface House_interface { void setStreetName (); String getStringName (); void showStreetName (); void setOccupantName (); String getOccupantName (); void showOccupantName (); void setStreetNumber(Double streetNumber); Double getStreetNumber(); }

7 Interfaces in Java, 2 nd example interface package building; public class House implements Building_interface, House_interface { /* code goes here for all methods in all interfaces that are listed */ /* as before can also include other methods */ }

8 Interfaces and Multiple Inheritance Interfaces are not part of the Java class hierarchy A class can inherit from exactly one class at most but it can implement more than one interface Objects of class House for example can be cast as objects of type House_interface House h1 = new House( ); House_interface hi1 = (House_interface)h1; Building_interface bi1 = (Builging_interface)h1; In other classes we can write methods that take a House_interface object as an argument. We can then use a House object as a valid argument by casting it to the relevant type as above.

9 Inheritance A Java class can be derived from other classes, thereby inheriting fields and methods from those classes. Class derived from other class is called a subclass, or extended class, or child class. Class the subclass is derived from is called a superclass, base class or parent class. Every class has exactly one direct superclass In the absence of any explicit superclass, every class is implicitly a subclass of Object. Except the Object class, which has no superclass

10 Inheritance To create a new class when there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to reinvent the wheel. Subclass inherits all public and protected fields, methods and nested inner classes of superclass Does NOT inherit any constructors Superclass constructors can be invoked from a subclass constructor

11 Inheritance, Example House class again, but this time we resuse the code from the Building class public class House extends Building implements Building_interface, House_interface { /* only need to implement the * House_interface methods now. * All Building_interface methods * inherited from Building class */ } Any House object has methods from Building class and methods added in House class. Refer to inhereted methods in exactly same way as instance methods. E.g for House object h1 we can call getRooms with h1. getRooms() even though getRooms is a Building method.

12 Building Example Informal UML class diagram showing basic hierarchy in Building project

13 Calling superclass constructor in constructor method package building; public class RentedHouse extends House { private int rent; private String landlord; /** Creates a new instance of RentedHouse */ public RentedHouse(String streetName, Double streetNumber, String occupantName, int rent, String landlord) { super (streetName, streetNumber, occupantName); this.rent = rent; this.landlord = landlord; } /* and so on */ } Call to constructor method of superclass: House Must be on first line of constructor method body Have not declared the class as implementing any interfaces in this example

14 Inheritance If declare a field in the subclass with same name as one in the superclass then superclass field is hidden by new field. Same comment as above goes for methods, overwritting in subclass hides the ones in the superclass. Can declare new fields in subclass that are not in superclass. You can declare new methods and fields in the subclass that are not in the superclass. Subclass does not inherit private members of its parent class, but can still acces them with get methods if they were implemented. Subclass objects can always be cast to have type of superclass: RentedHouse rh1 = new RentedHouse(); House h1 = (House)rh1; Hence any method that takes a House object as argument can be given a RentedHouse object when it is cast as a House.

15 Inheritance, using super keyword public class RentedHouse extends House { private int rent; private String landlord; private static String tax_occupant = " house empty"; private boolean tax_return = false; public RentedHouse(String streetName, Double streetNumber, String occupantName, int rent, String landlord) { super(streetName, streetNumber, occupantName); this.rent = rent; this.landlord = landlord; } public String getOccupantName () { if (tax_return) { return tax_occupant; } else { return super.getOccupantName(); } Have overwritten method in superclass, which hides it Can still call hidden super class method by using keyword super as object identifier. That then calls the current object but as if it is of the type for the superclass.

16 Object as a Superclass The Object class is the root of the class hierarchy Hence any method for the Object class is available to any object of any other class (of course some of them may have been overwritten). public boolean equals(Object obj) public final Class getClass() public int hashCode() public String toString()

17 Class getClass example public static void main(String args[ ]) { /* Go and do some stuff */ House h1 = new House ("Olderman St", 12.0, "John Cumbersome") ; Class cl1 = h1.getClass(); String house_class_str = cl1.getCanonicalName(); String object_string = h1.toString(); }

18 Object equals method Compares two object references to test if they refer to the same object. Usually needs to be overwritten to make sense. public static void main(String args[ ]) { /* Go and do some stuff */ House h1 = new House ("Olderman St", 12.0, "John Cumbersome") ; House h2 = new House ("Olderman St", 12.0, "John Cumbersome") ; boolean tst = h1.equals(h2); } Will return false

19 Writing Final Classes and Methods Can prevent a class being extended by declaring it final Similarly any individual method can be declared final to stop it being overwritten in any subclass public final String getOccupantName () { if (tax_return) { return tax_occupant; } else { return super.getOccupantName(); }

20 Abstract Classes Sometimes we want a class that is half way between an interface and a proper class It specifies some methods to be implemented in subclasses It gives the implementation for some methods as well An abstract class does just this job Abstract class is extended to a concrete class rather than be implemented A subclass of an abstract class must then implement the abstract methods whilst it can inherit those methods that have already been implemented

21 Abstract class example, Building class public abstract class Building implements Building_interface { can still have an abstract class implement an interface if we want public Vector getRooms() { return rooms; } /* and so on for other implemented methods */ abstract void showStreetName (); abstract void showOccupantName (); } for abstract methods just add signature with abstract keyword These MUST be implemented in subclass

22 Abstract class Where an abstract class has every method declared abstract and no instance fields then best to change it to an interface instead That way any other class can still be a subclass and implement this interface as well.

23