// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");

Slides:



Advertisements
Similar presentations
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
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.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
CC1007NI: Further Programming Week Dhruba Sen Module Leader (Islington College)
Inheritance Inheritance is the process of using features (both attributes and methods) from an existing class. The existing class is called the superclass.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Programming Languages and Paradigms Object-Oriented Programming.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction Object.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
// Java0601.java // This program demonstrates that the methods of a class are not always // accessible, like they were with the class. In this case an.
Inheritance Inheritance is the process of using features (both attributes and methods) from an existing class. The existing class is called the superclass.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and 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.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
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 and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
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.
Java Collection Hierarchy Collection interface List subinterface Set subinterface ArrayList class LinkedList class HashSet class TreeSet class.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Inheritance Object Oriented.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Exposure Java 2011 APCS Edition
Web Design & Development Lecture 9
A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism CSC 202.
Sixth Lecture ArrayList Abstract Class and Interface
Inheritance and Polymorphism
Chapter 9 Inheritance and Polymorphism
Interface.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Interfaces.
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
AP Computer Science DYRT Quiz
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n"); Bank tom = new Bank(5000.0, ); Bank sue = new Bank(3000.0, ); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom's savings balance: " + tom.getSavingsBalance()); System.out.println("Sue's checking balance: " + sue.getCheckingBalance()); System.out.println("Sue's savings balance: " + sue.getSavingsBalance()); System.out.println(); System.out.println("Tom makes a $ checking withdrawal"); tom.makeCheckingWithdrawal(1000.0); System.out.println("Tom makes a $ savings withdrawal"); tom.makeSavingsWithdrawal(2000.0); System.out.println("Sue makes a $ checking deposit"); sue.makeCheckingDeposit(1500.0); System.out.println("Sue makes a $ savings deposit"); sue.makeSavingsDeposit(3000.0); System.out.println(); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom's savings balance: " + tom.getSavingsBalance()); System.out.println("Sue's checking balance: " + sue.getCheckingBalance()); System.out.println("Sue's savings balance: " + sue.getSavingsBalance()); System.out.println(); }

class Bank { private double checking; private double savings; public Bank() { checking = 0.0; savings = 0.0; } public Bank(double c, double s) { checking = c; savings = s; } public double getCheckingBalance() { return checking; } public double getSavingsBalance() { return savings; } public void makeCheckingDeposit(double amount) { checking += amount; } public void makeSavingsDeposit(double amount) { savings += amount; } public void makeCheckingWithdrawal(double amount) { checking -= amount; } public void makeSavingsWithdrawal(double amount) { savings -= amount; } public void closeCheckingAccount() { checking = 0; } public void closeSavingsAccount() { savings = 0; } }

// Java2102.java // This program uses a simplified class, which will be used for future // program examples in this chapter. public class Java2102 { public static void main (String args[]) { System.out.println("\nJAVA2102.JAVA\n"); Bank tom = new Bank(5000.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $ checking deposit"); tom.makeCheckingDeposit(1500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $ checking withdrawal"); tom.makeCheckingWithdrawal(2500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println(); } class Bank { private double checking; public Bank(double c){ checking = c;} public double getCheckingBalance(){ return checking; } public void makeCheckingDeposit(double amount){ checking += amount; } public void makeCheckingWithdrawal(double amount){ checking -= amount; } }

// Java2103.java // The former class is now a interface. // Only the method headings are shown. The program does not compile. public class Java2103 { public static void main (String args[]) { System.out.println("\nJAVA2103.JAVA\n"); Bank tom = new Bank(); System.out.println(); } interface Bank { public double getCheckingBalance(); public void makeCheckingDeposit(double amount); public void makeCheckingWithdrawal(double amount); }

Program Differences Typical ProgramJava2103.java Uses class Uses interface Has methods with program statements No statements, only method headings Methods headings have no semi-colons Method headings have semi- colons Class has a constructorThere is no constructor There are fields to store dataThere are no fields

Java Interfaces A Java Interface provides a group of method signatures that will be available for any client of a class that implements the interface. Implementation details of the interface methods are neither required nor desired at the interface level.

Java Collection Hierarchy Collection Interface List Interface Set Interface ArrayList class LinkedList class HashSet class TreeSet class

Collections A collection is a group of objects.

Linear Collections A linear collection stores its elements in a specific order. Linear collections can have duplicate elements.

Lists A list is a linear collection that allows access to any element in the list. Examples of list data structures are Java static arrays, ArrayLists, Strings and Files.

Unordered Collections An unordered collection stores elements without order.

Bags A bag is an unordered collection that can have duplicate elements.

Sets A set is an unordered collection without any duplicate elements.

// Java2104.java // The class implements the interface. The program now compiles and executes. public class Java2104 { public static void main (String args[]) { System.out.println("\nJAVA2104.JAVA\n"); MyBank tom = new MyBank(5000.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $ checking deposit"); tom.makeCheckingDeposit(1500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $ checking withdrawal"); tom.makeCheckingWithdrawal(2500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println(); } interface Bank { public double getCheckingBalance(); public void makeCheckingDeposit(double amount); public void makeCheckingWithdrawal(double amount); } class MyBank implements Bank { private double checking; public MyBank(double c){ checking = c;} public double getCheckingBalance(){ return checking; } public void makeCheckingDeposit(double amount){ checking += amount; } public void makeCheckingWithdrawal(double amount){ checking -= amount; } }

// Java2105.java // An interface is "abstract" and its methods are also "abstract". The keyword is optional. public class Java2105 { public static void main (String args[]) { System.out.println("\nJAVA2105.JAVA\n"); MyBank tom = new MyBank(5000.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $ checking deposit"); tom.makeCheckingDeposit(1500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $ checking withdrawal"); tom.makeCheckingWithdrawal(2500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println(); } abstract interface Bank { public abstract double getCheckingBalance(); public abstract void makeCheckingDeposit(double amount); public abstract void makeCheckingWithdrawal(double amount); } class MyBank implements Bank { private double checking; public MyBank(double c){ checking = c;} public double getCheckingBalance(){ return checking; } public void makeCheckingDeposit(double amount){ checking += amount; } public void makeCheckingWithdrawal(double amount){ checking -= amount; } }

Implementation Rule A class, which implements an interface, must implement every method declared in the interface.

// Java2106.java // This program partially implements the class. Now the program does not compile. public class Java2106 { public static void main (String args[]) { System.out.println("\nJAVA2106.JAVA\n"); MyBank tom = new MyBank(5000.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $ checking deposit"); tom.makeCheckingDeposit(1500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $ checking withdrawal"); tom.makeCheckingWithdrawal(2500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println(); } abstract interface Bank { public abstract double getCheckingBalance(); public abstract void makeCheckingDeposit(double amount); public abstract void makeCheckingWithdrawal(double amount); } class MyBank implements Bank { private double checking; public MyBank(double c){ checking = c;} public void makeCheckingDeposit(double amount){ checking += amount; } public void makeCheckingWithdrawal(double amount){ checking -= amount; } }

// Java2107.java This program demonstrates that it is possible to implement an interface //and define additional methods that are not declared in the interface. public class Java2107 { public static void main (String args[]) { System.out.println("\nJAVA2107.JAVA\n"); MyBank tom = new MyBank(5000.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $ checking deposit"); tom.makeCheckingDeposit(1500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $ checking withdrawal"); tom.makeCheckingWithdrawal(2500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); tom.closeAccount(); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println(); } abstract interface Bank { public abstract double getCheckingBalance(); public abstract void makeCheckingDeposit(double amount); public abstract void makeCheckingWithdrawal(double amount); } class MyBank implements Bank { private double checking; public MyBank(double c){ checking = c;} public double getCheckingBalance(){ return checking; } public void makeCheckingDeposit(double amount){ checking += amount; } public void makeCheckingWithdrawal(double amount){ checking -= amount; } public void closeAccount(){ checking = 0; } }

// Java2108.java // This program shows how one class, can implement two // interfaces and. public class Java2108 { public static void main (String args[]) { System.out.println("\nJAVA2108.JAVA\n"); BankAccounts tom = new BankAccounts(5000.0,7500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $ checking deposit"); tom.makeCheckingDeposit(1500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $ checking withdrawal"); tom.makeCheckingWithdrawal(2500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println(); System.out.println("Tom's savings balance: " + tom.getSavingsBalance()); System.out.println("Tom makes a $ savings deposit"); tom.makeSavingsDeposit(1500.0); System.out.println("Tom's savings balance: " + tom.getSavingsBalance()); System.out.println("Tom makes a $ savings withdrawal"); tom.makeSavingsWithdrawal(2500.0); System.out.println("Tom's savings balance: " + tom.getSavingsBalance()); System.out.println(); }

abstract interface Checking { public abstract double getCheckingBalance(); public abstract void makeCheckingDeposit(double amount); public abstract void makeCheckingWithdrawal(double amount); } abstract interface Savings { public abstract double getSavingsBalance(); public abstract void makeSavingsDeposit(double amount); public abstract void makeSavingsWithdrawal(double amount); } class BankAccounts implements Checking,Savings { private double checking; private double savings; public BankAccounts(double c, double s){ checking = c; savings = s; } public double getCheckingBalance(){ return checking; } public double getSavingsBalance(){ return savings; } public void makeCheckingDeposit(double amount){ checking += amount; } public void makeSavingsDeposit(double amount){ savings += amount; } public void makeCheckingWithdrawal(double amount){ checking -= amount; } public void makeSavingsWithdrawal(double amount){ savings -= amount; } }

// Java2109.java // This program shows that it is possible to have a field in an interface, but it // must be final and initialized. public class Java2109 { public static void main (String args[]) { System.out.println("\nJAVA2109.JAVA\n"); MyBank tom = new MyBank(5000.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $ checking deposit"); tom.makeCheckingDeposit(1500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $ checking withdrawal"); tom.makeCheckingWithdrawal(2500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Computing interest"); tom.computeInterest(); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println(); }

abstract interface Bank { public final double rate = 0.05; public abstract double getCheckingBalance(); public abstract void makeCheckingDeposit(double amount); public abstract void makeCheckingWithdrawal(double amount); public abstract void computeInterest(); } class MyBank implements Bank { private double checking; private double interest; public MyBank(double c) { checking = c; interest = 0.0; } public double getCheckingBalance(){ return checking; } public void makeCheckingDeposit(double amount){ checking += amount; } public void makeCheckingWithdrawal(double amount){ checking -= amount; } public void computeInterest() { interest = checking * rate; checking += interest; }

Using Fields in an Interface Fields may be used in an interface declaration. All fields must have an initialized value. Field values are constant and cannot be changed. The final keyword is optional. Final is implied.

// Java2110.java // This program uses an abstract class, rather than a interface. // There appears no difference between an abstract class and an interface. public class Java2110 { public static void main (String args[]) { System.out.println("\nJAVA2110.JAVA\n"); MyBank tom = new MyBank(5000.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $ checking deposit"); tom.makeCheckingDeposit(1500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $ checking withdrawal"); tom.makeCheckingWithdrawal(2500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println(); } abstract class Bank { public abstract double getCheckingBalance(); public abstract void makeCheckingDeposit(double amount); public abstract void makeCheckingWithdrawal(double amount); } class MyBank extends Bank { private double checking; public MyBank(double c){ checking = c;} public double getCheckingBalance(){ return checking; } public void makeCheckingDeposit(double amount){ checking += amount; } public void makeCheckingWithdrawal(double amount){ checking -= amount; } }

// Java2111.java // An abstract class can have both abstract members and concrete members. // An interface can only have abstract members. public class Java2111 { public static void main (String args[]) { System.out.println("\nJAVA2111.JAVA\n"); MyBank tom = new MyBank(5000.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $ checking deposit"); tom.makeCheckingDeposit(1500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println("Tom makes a $ checking withdrawal"); tom.makeCheckingWithdrawal(2500.0); System.out.println("Tom's checking balance: " + tom.getCheckingBalance()); System.out.println(); } abstract class Bank { protected double checking; protected Bank(double c){ checking = c;} public abstract double getCheckingBalance(); public abstract void makeCheckingDeposit(double amount); public abstract void makeCheckingWithdrawal(double amount); } class MyBank extends Bank { protected MyBank(double c){ super(c);} public double getCheckingBalance(){ return checking; } public void makeCheckingDeposit(double amount){ checking += amount; } public void makeCheckingWithdrawal(double amount){ checking -= amount; } }

Abstract Interfaces & Abstract Classes All methods of an interface must be abstract. Methods in an abstract class may be abstract or concrete.

Interfaces vs. Abstract Classes InterfaceAbstract Class Abstract methods onlyAbstract and concrete methods No constructor allowedCan have a constructor Needs a class to implement the interface Needs a subclass to implement the abstract methods Only final data fields are allowed Any data field is allowed Cannot instantiate an object

Inheritance vs. Implementation Inheritance of Classes A class is a blueprint for creating an object. An inherited class (subclass) is a modified blueprint for creating an specialized object.

Inheritance vs. Implementation Implementation of Interfaces A interface is something abstract. It is like a general idea you have before anyone can create the blueprints.

Polymorphism Polymorphism allows a single accessing feature, such as an operator, method or class identifier, to have many forms.

// Java2112.java // This program displays the information of three different classes with 3 different methods. public class Java2112 { public static void main (String args[]) { Mammal animal1 = new Mammal(); Bird animal2 = new Bird(); Fish animal3 = new Fish(); getData1(animal1); getData2(animal2); getData3(animal3); System.out.println(); } public static void getData1(Mammal obj) { System.out.println("A " + obj.getType() + obj.getMove()); } public static void getData2(Bird obj) { System.out.println("A " + obj.getType() + obj.getMove()); } public static void getData3(Fish obj) { System.out.println("A " + obj.getType() + obj.getMove()); }

abstract interface Animal { abstract String getType(); abstract String getMove(); } class Mammal implements Animal { String animalType; public Mammal(){ animalType = "Mammal"; } public String getType(){ return animalType; } public String getMove(){ return " walks."; } } class Bird implements Animal { String animalType; public Bird(){ animalType = "Bird"; } public String getType(){ return animalType; } public String getMove(){ return " flies."; } } class Fish implements Animal { String animalType; public Fish(){ animalType = "Fish"; } public String getType(){ return animalType; } public String getMove(){ return " swims."; } }

// Java2113.java // This program is almost identical to Java2112.java. // All three of the use (Animal obj) in the method heading. public class Java2113 { public static void main (String args[]) { System.out.println("\nJAVA2113.JAVA\n"); Mammal animal1 = new Mammal(); Bird animal2 = new Bird(); Fish animal3 = new Fish(); getData1(animal1); getData2(animal2); getData3(animal3); System.out.println(); } public static void getData1(Animal obj) { System.out.println("A " + obj.getType() + obj.getMove()); } public static void getData2(Animal obj) { System.out.println("A " + obj.getType() + obj.getMove()); } public static void getData3(Animal obj) { System.out.println("A " + obj.getType() + obj.getMove()); }

// Java2114.java // This program demonstrates "polymorphism". // The method displays the correct data polymorphically. public class Java2114 { public static void main (String args[]) { System.out.println("\nJAVA2114.JAVA\n"); Mammal animal1 = new Mammal(); Bird animal2 = new Bird(); Fish animal3 = new Fish(); getData(animal1); getData(animal2); getData(animal3); System.out.println(); } public static void getData(Animal obj) { System.out.println("A " + obj.getType() + obj.getMove()); }

// Java2115.java // This program demonstrates that polymorphism can be done // with abstract classes in the same manner as abstract interfaces. public class Java2115 { public static void main (String args[]) { System.out.println("\nJAVA2115.JAVA\n"); Mammal animal1 = new Mammal(); Bird animal2 = new Bird(); Fish animal3 = new Fish(); getData(animal1); getData(animal2); getData(animal3); System.out.println(); } public static void getData(Animal obj) { System.out.println("A " + obj.getType() + obj.getMove()); }

abstract class Animal { abstract String getType(); abstract String getMove(); } class Mammal extends Animal { String animalType; public Mammal(){ animalType = "Mammal"; } public String getType(){ return animalType; } public String getMove(){ return " walks."; } } class Bird extends Animal { String animalType; public Bird(){ animalType = "Bird"; } public String getType(){ return animalType; } public String getMove(){ return " flies."; } } class Fish extends Animal { String animalType; public Fish(){ animalType = "Fish"; } public String getType(){ return animalType;} public String getMove(){ return " swims."; } }

Polymorphism Steps Use the following steps to use a method polymorphically 1 Declare an interface or abstract superclass with the necessary abstract methods. 2 Implement the abstract methods in multiple classes using implementations appropriate to each class. 3 Declare some method that performs a common purpose for each of the methods, but the actual execution depends on the implementation of the specified class object. The parameter heading of this method needs to use the interface identifier or the super class identifier.

// Java2116.java // This program is almost identical to Java2115.java. // This time the method uses the class. // The program does not compile, even though every class extends // the class. public class Java2116 { public static void main (String args[]) { System.out.println("\nJAVA2116.JAVA\n"); Mammal animal1 = new Mammal(); Bird animal2 = new Bird(); Fish animal3 = new Fish(); getData(animal1); getData(animal2); getData(animal3); System.out.println(); } public static void getData(Object obj) { System.out.println("A " + obj.getType() + obj.getMove()); }

// Java2117.java // This program demonstrates that it is possible to use the // class for polymorphism, but types casting must be used // to the class before the program functions properly. public class Java2117 { public static void main (String args[]) { System.out.println("\nJAVA2117.JAVA\n"); Mammal animal1 = new Mammal(); Bird animal2 = new Bird(); Fish animal3 = new Fish(); getData(animal1); getData(animal2); getData(animal3); System.out.println(); } public static void getData(Object obj) { System.out.println("A " + ((Animal)obj).getType() + ((Animal)obj).getMove()); }

Using the Object Class with Polymorphism Java allows the use of a higher class or interface in the parameter signature of a method heading. This is precisely what makes polymorphism possible. The higher level class or interface must have abstract methods declared for polymorphism to work properly. It is possible to use a higher class or interface, such as Object for a polymorphic method. However, if the required methods are not declared in the higher class or interface, then it becomes necessary to type cast to a class or interface, which does declare the required methods.