Polymorphism idea
Download
1 / 20

Ex11 - PowerPoint PPT Presentation


  • 201 Views
  • Updated On :

Polymorphism Idea. The basic principle and aims of the generality and abstraction are: Reuse Interoperability The basic idea: A programmer uses a single, general interface, Java selects the correct method. Polymorphic Methods.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Ex11' - mike_john


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Polymorphism idea l.jpg

Polymorphism Idea

The basic principle and aims of the generality and abstraction are:

Reuse

Interoperability

The basic idea:

A programmer uses a single, general interface, Java selects the correct method.


Polymorphic methods l.jpg
Polymorphic Methods

  • 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. At runtime, the object responds to the message by executing the appropriate method.


Polymorphism l.jpg
Polymorphism

  • System.out.println(p);

  • is a message to the object referred to by p (Which message?):

    • If an Object is referenced by p, then toString method of Object class is invoked

    • If a Point is referenced by p, then toString method of Point class is invoked.

    • If ColoredPoint is referenced by p, then toString method of ColoredPoint class is invoked.


Static methods l.jpg
Static Methods

  • When we invoke a method on an object we always do it through a reference?

  • Static methods can be invoked using a class name! (but can also using a reference)

  • They are resolved at compile time, when we do not know which type of object is actually referenced. Therefore they depend on the type of reference.


Instanceof operator l.jpg
Instanceof Operator

  • You can restore the narrow point of view for objects, by using casting.

  • However at runtime you must know the true type of object referenced by some wider (base) reference (why?).

  • The instanceof operator can be used to query about the true type of the object you are referencing.


Rules of casting l.jpg
Rules of Casting

  • Casting let us restore a more specific point of view of an object.

  • Casting a reference should be done only after verifing the object is indeed of the type we cast to!

  • You cannot refer to an object through a Point reference unless this object is indeed a Point object!

  • If we try to cast a Point reference referring to a regular Point object, into a ColoredPoint, a ClassCastException will be thrown.

    Point p = new Point();

    ColoredPoint cp = (ColoredPoint )p;


Casting example l.jpg
Casting example

class Parent {}

class Child extends Parent {}

class Uncle {}

class Test {

public static void main( String args[] ) {

Object object;

Parent parent;

Child child = new Child();

Uncle uncle;

parent = child;

object = child;

parent = (Parent) object;

child = (Child) object;

uncle = (Uncle) object;

parent = (Parent)child;

parent = new Parent();

child = (Parent)child; parent = (Child)parent;

Object o = (Parent)child;

parent = new Child();

Parent p = (Parent)parent;

Child c = parent; }

}


Abstract classes l.jpg
Abstract Classes

  • We would like to describe abstract data types as classes. We use an abstraction which can sometimes be so general, that without filling the gaps it would make no sense to create an object from the class.

  • Shape, Animal, Vehicle ….


Abstract classes9 l.jpg
Abstract Classes

  • We would like to prevent the user from creating an object that makes no sense.

  • This can be accomplished by making all the methods print error messages, but that delays the information until run-time and requires reliable exhaustive testing on the user’s part.

  • It’s always better to catch problems at compile-time.


Abstract classes10 l.jpg
Abstract Classes

  • The java language allows a class designer to specify that a super class declares a method with no implementation.

  • A method with a missing implementation is called an abstract method as opposed to a concrete method with an implementation

  • The syntax of an abstract method is :

    abstract void foo();


Abstract classes11 l.jpg
Abstract Classes

  • A class with one or more abstract methods is called an Abstract class. The class must be qualified as abstract ,otherwise, the compiler gives you an error message.

  • A class which implements all its methods is a concrete class.

  • Note that you can define a class to be abstract although it has no abstract methods but this is done in rare cases.


Abstract classes12 l.jpg
Abstract Classes

  • The implementation of an abstract method must be supplied by a concrete sub class of the abstract class

  • The java compiler prevents the programmer from instantiating an abstract class

  • However the abstract class may have some concrete methods as well as constructors and attributes


Animals l.jpg
Animals

  • An animal defines a group of living things animals do have common features, but an instance of an animal does not male sense unless we fill the define the concrete feature of specified object.

  • Animals eat, move and reproduce


Abstract animal l.jpg
Abstract Animal

/**

* An Animal represents a living animal that can

* reproduce, eat and move

*/

public abstract class Animal {

protected int numberOfLegs;

/**

* Constructs a new Animal with a given number of legs

*/

public Animal(int numberOfLegs) {

this.numberOfLegs = numberOfLegs;

}

public abstract void eat();

public abstract Animal reproduce();

/**

* move the animal

*/

public void move() {

System.out.println(" this aniamal is moving with it's " + numberOfLegs + "legs");

}

}


Slide15 l.jpg
Pets

Pets are animals that human keep. A pet can be named and played with. Still this definition is somewhat general and A pet instance may not make sense.

public abstract class Pet extends Animal {

protected String name;

/**

* Constructs a new Pet with a given number of legs

* and a given name

*/

public Pet (int numberOfLegs, String name){

super(numberOfLegs);

this.name = name;

}

public void setName(String name){

this.name = name;

}

public String getName(){

return name;

}

public abstract void play();

}


Animal hierarchy l.jpg
Animal Hierarchy

Animal

Pet

Spider

Dog

Cat


Vector l.jpg
Vector

  • One of the most useful classes in the Java API is java.util.Vector.

  • A vector is an ordered collection of objects.

  • You may add or remove objects from the vector at any position and its size grows and shrinks as needed to accommodate adding and removing items.


Cats dogs l.jpg
Cats & Dogs…

  • class Cat {

  • public String toString() {

  • return new String(“meaw”);

  • }

  • }

  • class Dog {

  • public String toString() {

  • return new String(“bark”);

  • }

  • }

  • class Mouse {

  • public String toString() {

  • return new String(“squeak”);

  • }

  • }


Using a vector l.jpg
Using a Vector

class MouseTrap {public static void main(String[] args) {

Vector v = new Vector();

v.addElement(new Cat());

v.addElement(new Mouse());

v.addElement(new Dog());

v.addElement(new Mouse());

v.addElement(new String(“its raining”));

for (int i = 0; i < v.size(); i++)

System.out.println(v.elementAt(i));

catchTheMice(v);

}

private static void catchTheMice(Vector v) {

int i = 0;

while (i < v.size()) {

if (v.elementAt(i) instanceof Mouse) {

v.removeElementAt(i);

} else {

i++;

}

}

}


The vector holds object references l.jpg
The Vector Holds Object References

“a string”

Object

Mouse

String

Cat

Dog


ad