Introduction to Java 2 Programming

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

More on Classes Inheritance and Polymorphism
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
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.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
COP 3003 Object-Oriented Programming - Polymorphism Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
CISC6795: Spring Object-Oriented Programming: Polymorphism.
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.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Java Implementation: Part 3 Software Construction Lecture 8.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Object Oriented Software Development
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Inheritance and Access Control CS 162 (Summer 2009)
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Object Oriented Programming
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Introduction to Object-Oriented Programming Lesson 2.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
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.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Classes, Interfaces and Packages
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
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.
BY:- TOPS Technologies
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Advanced Programming in Java
Overriding Method.
Inheritance and Polymorphism
Inheritance in Java.
Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff…
Object Oriented Programming
More inheritance, Abstract Classes and Interfaces
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Abstract Classes AKEEL AHMED.
Fundaments of Game Design
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
Inheritance and Polymorphism
Inheritance Lakshmish Ramaswamy.
Presentation transcript:

Introduction to Java 2 Programming Lecture 6 Interfaces, Polymorphism

Overview Interfaces Static Binding Dynamic Binding Casting Exercises Polymorphism Casting Exercises

Interfaces Interfaces define a contract Contain methods and their signatures Can also include static final constants (and comments) But no implementation Very similar to abstract classes One interface can extend another, but An interface cannot extend a class A class cannot extend an interface Classes implement an interface

Defining Interfaces Use the interface keyword public interface Vehicle { public void turnLeft(); public void turnRight(); } Like abstract methods, the signature is terminated with a semi-colon

Implementing Interfaces Use the implements keyword public class MotorBike implements Vehicle { //include methods from Vehicle interface } Class must implement all methods of the interface OR declare itself to be abstract Classes can implement any number of interfaces public class MotorBike implements Vehicle, Motorised Possible to combine extends and implements public class MotorBike extends WheeledVehicle implements Vehicle, Motorised

Benefits of Interfaces Cleanly separates implementation of behaviour from description of the behaviour Means the implementation is easily changed Vehicle vehicle = new MotorBike(); // might become… Vehicle vehicle = new MotorCar(); Many OO systems are defined almost entirely of interfaces Describes how the system will function The actual implementation is introduced later

Overview Interfaces Static Binding Dynamic Binding Casting Exercises Polymorphism Casting Exercises

Binding Binding is what happens when a method invocation is bound to an implementation Involves lookup of the method in the class, or one or its parents Both method names and parameters are checked Binding can happen at two different times Compile time == static binding Run time == dynamic binding

Static Binding References have a type Objects have a type I.e. they refer to instances of a particular Java class Objects have a type I.e. they are instances of a particular Java class They are also instances of their super-class Inheritance describes an isA relationship E.g. a MotorBike isA MotorVehicle Static binding done by the compiler When it can determine the type of an object Method calls are bound to their implementation immediately

Static Binding Example 1 //class definition public class MotorBike { public void revEngine() {…} } //usage MotorBike bike = new MotorBike(); motorbike.revEngine();

Static Binding Example 2 public class MotorVehicle { public void start() {…} public void stop() {…} } public class MotorBike extends MotorVehicle() { //overridden public void revEngine() {…} //usage. Still statically bound MotorBike bike = new MotorBike(); motorbike.start();

Dynamic Binding Achieved at runtime When the class of an object cannot be determined at compile time Means the JVM (not the compiler) must bind a method call to its implementation Instances of a sub-class can be treated as if they were an instance of the parent class Therefore the compiler doesn’t know its type, just its base type.

Dynamic Binding Example 1 //reference is to base class MotorVehicle vehicle = new MotorBike(); //method is dynamically bound to MotorBike start method vehicle.start(); //remember all classes derive from Object Object object = new MotorBike(); object.toString();

Dynamic Binding Example 2 public interface ElectricalAppliance { public void turnOn(); public void turnOff(); } public class RemoteControl() { public static void turnApplianceOn(ElectricalAppliance appliance) { appliance.turnOn(); ElectricalAppliance appliance = …; RemoteControl.turnApplianceOn(appliance);

Dynamic Binding Example 2 public class HairDryer implements ElectricalAppliance { } public class Light implements ElectricalAppliance { ElectricalAppliance appliance = new HairDryer(); RemoteControl.turnApplianceOn(appliance); appliance = new Light();

References and Behaviours The type of the object determines its possible behaviours I.e. we define them in the class The object reference limits the behaviours we can invoke to those defined by the type of the reference

Dynamic Binding Example 3 public class HairDryer implements ElectricalAppliance { //other methods public void adjustTemperature(); } ElectricalAppliance appliance = new HairDryer(); //following won’t compile appliance.adjustTemperature();

Dynamic Binding Summary Whenever a reference refers to an interface or a base class, methods are dynamically bound Method implementation determined at runtime Dynamic Binding == Polymorphism Very powerful OO feature Allows the creation of “frameworks” Applications that are implemented around interfaces, but are customised by plugging in different implementations of those interfaces Very extensible

Overview Interfaces Static Binding Dynamic Binding Casting Exercises Polymorphism Casting Exercises

Checking an Objects Type Its possible to check the actual type of an object May want to check the real type, if we’ve only got a reference to an interface or base class Use the instanceof operator Must be applied to an object, tests whether it has a given type

Instanceof Example //class definition public class MotorBike implements Vehicle, Motorised { … } //code fragment Vehicle bike = new MotorBike(); if (bike instanceof MotorBike) { //do something } if (bike instanceof Motorised) {

Casting If we know the type, we can then “cast” the object Vehicle bike = new MotorBike(); if (bike instanceof MotorBike) { MotoBike bike = (MotorBike)bike; } If the object isn’t of that type, then an exception will be thrown Good idea to always check before casting, unless you’re absolutely sure!

Overview Interfaces Static Binding Dynamic Binding Casting Exercises Polymorphism Casting Exercises