Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 7.

Slides:



Advertisements
Similar presentations
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Advertisements

CS 211 Inheritance AAA.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
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.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Slide 1 of 66. Lecture H Lecture H - Inheritance Unit H1 - Introduction to Inheritance.
1 Inheritance in Java CS 3331 Fall Outline  Overloading  Inheritance and object initialization  Subtyping  Overriding  Hiding.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
Application development with Java Lecture 23. Polymorphism The Webster definition: – pol-y-mor-phism n. : a genetic variation that produces differing.
What is inheritance? It is the ability to create a new class from an existing class.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
1 Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Inheritance ndex.html ndex.htmland “Java.
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.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Web Design & Development Lecture 9
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Object-oriented Programming in Java
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Week 8 Lecture -3 Inheritance and Polymorphism
ATS Application Programming: Java Programming
Polymorphism 11-Nov-18.
Overriding Methods & Class Hierarchies
Inheritance in Java CS 3331 Fall 2009.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 7

Rina Overview Object class Final Polymorphism

Rina The Object Class Java defines the class java.lang.Object that is defined as a superclass for all classes. All classes directly or indirectly extend the Object class. If a class does not specify explicitly which class it is derived from, then it will be implicitly derived from class Object. All classes inherit Object’s methods.

Rina Point ColoredPoint Object Hierarchy Diagram We can depict the relationship between this classes in the following diagram, that is called class hierarchy diagram.

Rina The Object Class The following two class definitions are equivalent: class SomeClass { // some code } and class SomeClass extends Object { // some code } Because all classes derived from the Object.

Rina Methods of Object Class The Object class defines a set of methods that are inherited by all classes. Some of them are: equals(Object o), getClass(), toString() toString() method that is used whenever we want to get a String representation of an object. When you define a new class, you can override the toString() method in order to have a suitable representation of the new type of objects as Strings.

Rina Overriding toString() public class Point { private int x,y; public Point(int x, int y) {this.x=x; this.y=y; } public int getx() {return x;} public int gety() {return y;} public String toString() { return "(" + x + "," + y + ")"; } }

Rina Overriding toString() public class ColoredPoint extends Point { private Color color; public ColoredPoint(int x, int y,Color color) { super(x,y); this.color=color; } void setColor(Color color) { this.color = color; } public String toString() { return "(" + getx() + "," + gety() + "," + color + ")"; }

Rina Overriding toString() import java.awt.Color; class PrintingPointExample { public static void main(String[] args) { ColoredPoint p = new ColoredPoint(2,3,Color.red); System.out.println(p); }

Rina Method equals(Object o) Indicates whether some other object is "equal to" this one. For any reference values x and y, this method returns true if and only if two separate objects x and y refer are of the same type and contain the same data. Note that is is not the same as comparing two object references using the == operator. That test simply determines if two object references point to the same object.

Rina Overloading vs. Overriding Overloading deals with multiple methods in the same class with the same name but different signatures. Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature. Overloading lets you define a similar operation in different ways for different data. Overriding lets you define a similar operation in different ways for different object types.

Rina Multiple inheritance Multiple inheritance means that a derived class can have more than one parent. Exist in some-object oriented languages, like C++. Java’s approach to inheritance is single inheritance. Only one extends allowed. Usually you can avoid the need for multiple inheritance with good design of the class hierarchy. But there are other solutions in Java (through interfaces).

Rina Final modifier final modifier can be used to make class childless. Syntax: final class SubClass extends SuperClass { //…} Means SubClass is the last class in class hierarchy. final methods cannot be redefined. –Allows the virtual machine to optimize the byte code. –For security reasons.

Rina Final example class Feline { boolean isHungry = true; void speak() {} void call() { System.out.println("here kitty, kitty..."); if (_isHungry) speak(); } final class Cat extends Feline { String race; void speak() { System.out.println("meow!"); } } final class Lion extends Feline { void speak() { System.out.println("roar!"); } void hunt() {...} } A final class cannot be derived further.

Rina Polymorphism The Webster definition: pol-y-mor-phism n. 1. the state or condition of being polymorphous. 2. a. genetic variation that produces differing characteristics in individuals of the same population or species. pol-y-mor-phous adj. 1. having, assuming, or passing through many or various forms, stages, or the like;

Rina Polymorphism The basic principle and aims of the generality and abstraction are: reuse interoperability The basic idea: Programmer uses a single, general interface Java selects the correct method. or Write once use anywhere.

Rina Polymorphism ColoredPoint is a Point. Point is an Object. ColoredPoint can be used by any code designed to work with Point objects. If a method expects a Point object you can hand it a ColoredPoint object and it will work. This means that ColoredPoint object can be used as both a Point object and a ColoredPoint object (ColoredPoint can have many forms).

Rina Polymorphism We can view a RestrictedFile object from 3 different points of views: As a RestrictedFile. This is the most narrow point of view (the most specific). This point of view ‘sees’ the full functionality of the object. As a File. This is a wider point of view (a less specific one). We forget about the special characteristics the object has as a RestrictedFile (we can only open and close the file). As an plain Object.

Rina Polymorphism We view an object by using an object reference. A variable of type ‘reference to File’ can only refer to an object which is a File. File f = new File(“story.txt”); But a RestrictedFile is also a File, so f can also refer to a RestrictedFile object. File f = new RestrictedFile(“visa.dat”); The type of the reference we use determines the point of view we will have on the object.

Rina Polymorphism If we refer to a RestrictedFile object using a RestrictedFile reference we have the RestrictedFile point of view. We see all the methods that are defined in RestrictedFile and up the hierarchy tree. 4RestrictedFile f = new RestrictedFile(“visa.dat”,12345); 4f.close(); 4f.lock(); 4f.unlock(12345); 4String s = f.toString();

Rina Polymorphism If we refer to a RestrictedFile object using a File reference we have the File point of view - which lets us use only methods that are defined in class File and up the hierarchy tree. 4File f = new RestrictedFile (“visa.dat”,12345); 4f.close(); 8f.lock(); 8f.unlock(12345); 4String s = f.toString();

Rina Polymorphism If we refer to a RestrictedFile object using an Object reference we have the Object point of view - which let us see only methods that are defined in class Object. 4Object f = new RestrictedFile (“visa.dat”,12345); 8f.close(); 8f.lock(); 8f.unlock(12345); 4String s = f.toString();

Rina Polymorphism RestrictedFile ...  isOpen  isLocked  key  toString() ...  isOpen()  open()  close()  lock()  unlock(key)  isLocked()

Rina RestrictedFile reference RestrictedFile point of view File reference File point of view Widening Changing our point of view of an object, to a wider one (a less specific one) is called widening. File file = new RestrictedFile((“visa”, 1234);

Rina Polymorphism A method is polymorphic if the action performed by the method depends on the actual type of the object to which the method is applied. In OOP calling a method is often referred to as sending a message to an object. The object responds to the message by executing the appropriate method. Polymorphism just means that different objects can respond to the same message in different ways.

Rina Polymorphic method The statement System.out.println(point); is a message to the object referred to by point. Since that object knows what type of object it is, it knows how it should respond to the message: If point is a reference to Point then toString method of Point class is invoked. If point is a reference to ColoredPoint then toString method of ColoredPoint class is invoked.

Rina Example public String toString() { return "(" + x + "," + y + ")"; } public String toString() { return "(" + getx() + "," + gety() + "," + color + ")"; }

Rina Java Methods are Virtual We say that methods in Java are virtual, which means that the type of method which will be invoked depends on the type of object (and not on the type of reference), and this type is determined at runtime. There are languages that use different mechanisms for method invocation.

Rina Example class PointExample { public static void main(String[] args) { Point point; if (Math.random() >= 0.5) { point = new Point(3,4);} else { point = new ColoredPoint(5,6,Color.red);} System.out.println(point); } What will be printed if the number tossed is greater than 0.5? (3,4)

Rina Any Questions?