OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
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.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
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.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
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.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
What is inheritance? It is the ability to create a new class from an existing class.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
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
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
Polymorphism, Virtual Methods and Interfaces Version 1.1.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
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.
Classes, Interfaces and Packages
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
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.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Object Oriented Programming
CSC 143 Inheritance.
Interface.
Extending Classes.
Java Programming Language
Polymorphism and access control
Polymorphism, Abstract Classes & Interfaces
Java – Inheritance.
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Inheritance Lakshmish Ramaswamy.
Presentation transcript:

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path

The toString() Method: The toString() method is declared in the Object Class. It is commonly used by objects to return a String representation of themselves to display their current state. Because every Class in Java inherits from Object, every Class has access to the toString() method. The toString() method is commonly overridden in sub classes so that developers can decide how objects will be textually displayed at run time. Objects are commonly displayed textually through the System.out.println command or through List Boxes in GUI environments.

OO Concepts Polymorphism: It is important to understand what happens when a method call is made to objects of various types in an inheritance hierarchy. When you a call a method of a subclass using certain parameters, here is what happens: The subclass checks to see if it has that method with exactly the parameters specified. If it does it uses it. If the method is not found the super class is searched. If the method is found in a super class the method is executed. If the method is not found a compile time error occurs.

OO Concepts Polymorphism: An objects ability to decide what method to apply to itself in the inheritance hierarchy is called polymorphism. Methods with same signature (name and argument list) defined in an inheritance hierarchy is called method overriding. Objects in an inheritance hierarchy with overridden methods can react differently to the same method call. Polymorphism is also the ability of objects in an inheritance hierarchy to react differently to the same method call.

OO Concepts Dynamic Binding: Dynamic binding is very closely related to polymorphism in Java. Dynamic binding is the process of resolving the reference to a method at run time. When you call a method that has been overridden by at least one subclass in an inheritance hierarchy, the JVM determines which version of the method to call. You do not have to do any special coding to take advantage of dynamic binding because it is automatic.

OO Concepts Interfaces An interface is a special kind a Class that is used as a design template to specify what kind of behavior a Class must implement. An interface is a group of constants and method declarations that define the form of a class. However, interfaces provide no method implementation. In essence, an interface allows you to specify what a class must do, but not how it will get done. Abstract Classes use abstract methods to ensure that any Class inherited from the Abstract Class must implement that method.

OO Concepts Interfaces Interfaces are similar in that they define public abstract methods that provide no implementation. The Class implementing the interface is also forced to implement these methods. Interfaces can have fields, but they must be declared as static and final. If you omit the static and final qualifiers Java provides them for you.

OO Concepts Syntax for Interfaces: [public] interface interface_name [extends interface_name] { /*Declare fields and methods*/ } How do Classes Implement Interfaces? public class clasName implements interfaceName[,interfaceName] { /*Class Body*/ }

Class Path & Packages The CLASSPATH variable is how we tell applications written in the Java programming language where to look for user Classes. The Java interpreter (java) knows where to find the Core Java Classes that come with Java. The Java interpreter locates these classes needed to run your code by using the CLASPATH environment variable. We have used the Java Class String numerous times in our programs so far. The interpreter was able to find the String class because it is in the system environment variable called CLASSPATH.