January 31 Is-a Versus Has-a Polymorphism Abstract & Final Class/Method.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

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.
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 Java permits you to use your user defined classes to create programs using inheritance.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Overview of Java (continue). Announcements You should have access to your repositories and HW0 If you have problems getting HW0, let me know If you’ve.
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,
Object-Oriented Design CSC 212. Announcements Ask more questions! If I am not making myself clear, it is your opportunity to explain what is confusing.
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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
Chapter 10: Inheritance and Polymorphism
Chapter 10 Classes Continued
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
LECTURE 07 Programming using C# Inheritance
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
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.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
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.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
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 Fall 2010.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Session 24 Chapter 12: Polymorphism. Polymorphism polymorphism comes from the Greek root for “many forms” polymorphism is about how we can use different.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Coming up: Inheritance
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Session 18 Lab 9 Re-cap, Chapter 12: Polymorphism & Using Sound in Java Applications.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Inheritance ndex.html ndex.htmland “Java.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Object-Oriented Programming: Polymorphism Chapter 10.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Inheritance and Polymorphism
Polymorphism CT1513.
Polymorphism Polymorphism - Greek for “many forms”
Object Oriented Programming
Chapter 14 Abstract Classes and Interfaces
Presentation transcript:

January 31 Is-a Versus Has-a Polymorphism Abstract & Final Class/Method

Is-a versus Has-a zDon’t confuse inheritance with having a data field that is another class(composition) zDeclaring a class inside another just sets up a reference variable to the class with no relationship zThe difference between Inheritance and Composition becomes clear by asking Is-a ? Or Has-a ? question zWhat would you say, a car has an engine or a car is an engine? zWe would say a Manager is an Employee.

Extended Class Is-a Super class type Class A method a1() method a2() Class B method b1() method b2() extends … B b = new B(); b.a1(); // OK!!! B.b2(); // OK!!! A a = new A(); a.b2(); // Not OK!!! A is not same type as B

No Multiple Inheritance zMultiple inheritance means having more than one immediate super class. class FlyingMachine class FloatingVessel class SeaPlane zSorry, This is not allowed in Java!!! zMultiple inheritance may give us total mess. Imagine two super class have the same method with totally different behavior.

Rationale Behind Inheritance(subclassing) zClass Reuse. Inheritance inherently provides class reuse. zProvides logical way of implementing generic type handling. What if you want to treat different types of objects as one common type? void promote(Employee worker) { … } promote() can take in any objects of Employee Manager or Executive types. zPolymorphism is for ensuring individuality of each subclass.

Polymorphism(Name Reuse) zOverloading (easy and weak polymorphism) In any class, we can use the same name for several different methods. zOverriding A subclass class can have a method with the same signature as a method in the superclass. zOverloading is resolved at compile time. zOverriding is resolved at runtime. What does this mean?

Upcasting & Overriding zUpcasting is always safe and allowed. Manager manager = new Manager(“hoony,”2m”,”12/1/00”); Employee employee = manager; zNow manager is upcasted to Employee, consider employee.raiseSalary(0.5); Which raiseSalary() will be invoked, that of Manager or Employee? zThe real type is resolved at runtime.Late-binding True Beauty of Polymorphism!!! manager is created as Manager Type!!!

Forcing Overriding Off: Final zfinal class means “No one can extend this class!!!” Ex) java.lang.Math public final class Math {... public static native double sin(double a); zSimilarly, final method means “No subclass can override this method!!!”

Forcing Overriding: Abstract zabstract class means “I am incomplete, please extend me by overriding all my abstract methods!!!” zabstract method has no body. Its purpose is to force some subclass to override it and provide a concrete implementation of it. zabstract class has zero or more of its methods are abstract.

abstract class Shape { public void setColor(Color color) { … } public void setVisible(boolean t) { … } public abstract void draw(); } class Circle extends Shape { public void draw() { // draw circle here!!! } abstract class Trapezoid extends Shape { private int sides = 4; public abstract void draw(); } class Rectangle extends Trapezoid { public final void draw() { // draw four sides using this // method only!!! } class Square extends Rectangle { public void void draw() { // Sorry!!! This overriding is not // Allowed!!! }