Object Oriented Programming

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
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.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
ITEC200 – Week03 Inheritance and Class Hierarchies.
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.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
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 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
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.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance-Basics.
Inheritance and Polymorphism
Object-Oriented Programming
Inheritance in Java.
Object-Oriented Programming: Polymorphism
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
ATS Application Programming: Java Programming
Designing for Inheritance
Modern Programming Tools And Techniques-I Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
CSC 143 Inheritance.
Inheritance Basics Programming with Inheritance
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Extending Classes.
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
Polymorphism Polymorphism
Computer Programming with JAVA
Java – Inheritance.
Pointers Dr. Bhargavi Goswami Department of Computer Science
Polymorphism CT1513.
Polymorphism Polymorphism - Greek for “many forms”
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Java Inheritance.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance and Polymorphism
Object Oriented Programming
Object Oriented Programming
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
C++ Object Oriented 1.
Presentation transcript:

Object Oriented Programming

Topics To Be Covered Today Method Overriding Abstract Classes 3 uses of Final

Method Overriding When a method of a sub-class has the same name and type as a method of the super-class, we say that this method is overridden. When an overridden method is called from within the sub-class: it will always refer to the sub-class method super-class method is hidden

Example: Hiding with Overriding

Example: Hiding with Overriding

Example: Hiding with Overriding When show() is invoked on an object of type B, the version of show() defined in B is used: The version of show() in A is hidden through overriding.

Super and Overriding The hidden super-class method may be invoked using super: The super-class version of show() is called within the sub-class’s version.

Overriding versus Overloading Method overriding occurs only when the names and types of the two methods (super-class and sub-class methods) are identical. If not identical, the two methods are simply overloaded:

Overriding versus Overloading The show() method in B takes a String parameter, while the show() method in A takes no parameters:

Overriding versus Overloading The two invocations of show() are resolved through the number of arguments (zero versus one):

Dynamic Method Invocation Overriding is the basis for dynamic method dispatch – a call to an overridden method is resolved at run-time, rather than compile-time. Method overriding allows for dynamic method invocation: an overridden method is called through the super-class variable Java determines which version of that method to execute based on the type of the referred object at the time the call occurs when different types of objects are referred, different versions of the overridden method will be called.

Example: Dynamic Invocation A super-class A:

Example: Dynamic Invocation Two sub-classes B and C: B and C override the A’s callme() method.

Example: Dynamic Invocation Overridden method is invoked through the variable of the super-class type. Each time, the version of the callme() method executed depends on the type of the object being referred to at the time of the call:

Polymorphism again One interface, many behaviors: super-class defines common methods for sub-classes sub-class provides specific implementations for some of the methods of the super-class A combination of inheritance and overriding – sub-classes retain flexibility to define their own methods, yet they still have to follow a consistent interface.

Example: Polymorphism A class that stores the dimensions of various 2- dimensional objects:

Example: Polymorphism Rectangle is a sub-class of Figure:

Example: Polymorphism Triangle is a sub-class of Figure:

Example: Polymorphism Invoked through the Figure variable and overridden in their respective subclasses, the area() method returns the area of the invoking object:

Abstract Method Inheritance allows a sub-class to override the methods of its super-class. In fact, a super-class may altogether leave the implementation details of a method and declare such a method abstract: abstract type name(parameter-list); Two kinds of methods: concrete – may be overridden by sub-classes abstract – must be overridden by sub-classes It is illegal to define abstract constructors or static methods.

Example: Abstract Method The area method cannot compute the area of an arbitrary figure: Instead, area should be defined abstract in Figure:

Abstract Class A class that contains an abstract method must be itself declared abstract: An abstract class has no instances - it is illegal to use the new operator: It is legal to define variables of the abstract class type.

Abstract Sub-class A sub-class of an abstract class: implements all abstract methods of its super-class, or is also declared as an abstract class

Abstract and Concrete Class Abstract super-class, concrete sub-class:

Abstract and Concrete Class Calling concrete and overridden abstract methods:

Example: Abstract Class Figure is an abstract class; it contains an abstract area method:

Example: Abstract Class Rectangle is concrete – it provides a concrete implementation for area:

Example: Abstract Class Triangle is concrete – it provides a concrete implementation for area:

Example: Abstract Class Invoked through the Figure variable and overridden in their respective subclasses, the area() method returns the area of the invoking object:

Abstract Class Reference It is illegal to create objects of the abstract class: Figure f = new Figure(10, 10); It is legal to create a variable with the abstract class type: Figure figref; Later, figref may be used to assign references to any object of a concrete sub-class of Figure (e.g. Rectangle) and to invoke methods of this class: Rectangle r = new Rectangle(9, 5); figref = r; System.out.println(figref.area());

Uses of Final The final keyword has three uses: declare a variable which value cannot change after initialization declare a method which cannot be overridden in sub-classes declare a class which cannot have any sub- classes

Preventing Overriding with final A method declared final cannot be overridden in any sub-class: This class declaration is illegal:

Final and Early Binding Two types of method invocation: early binding – method call is decided at compile-time late binding – method call is decided at run-time By default, method calls are resolved at run- time. As a final method cannot be overridden, their invocations are resolved at compile-time. This is one way to improve performance of a method call.

Preventing Inheritance with final A class declared final cannot be inherited – has no sub-classes. final class A { … } This class declaration is considered illegal: class B extends A { … } Declaring a class final implicitly declares all its methods final. It is illegal to declare a class as both abstract and final.

Object Class Object class is a super-class of all Java classes: Object is the root of the Java inheritance hierarchy. A variable of the Object type may refer to objects of any class. As arrays are implemented as objects, it may also refer to any array.

Questions?