Understanding Object-Oriented Programming Concepts and Polymorphism Using Java and C#

Slide Note
Embed
Share

Explore key Object-Oriented Programming concepts like inheritance, method overriding, abstract classes, interfaces, and polymorphism through examples in Java and C#. Delve into class structures, casting, and handling different object types at runtime. Learn how to effectively utilize these principles in your programming projects.


Uploaded on Sep 29, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Module 3 - Part 5 Putting it all together

  2. What weve seen so far Inheritance Overriding methods Abstract classes Interfaces Polymorphism ArrayLists What we still need to see: 1) casting and 2) an example that ties all this together

  3. The Example class Mammal - <attributes> + breathe( ) class Cat class Dog <<interface>> Talkable + talk( ) +breathe <<override>> +talk <<override>> +doDogStuff( )

  4. In Code Java abstract class Mammal { void breathe() { System.out.println ("Generic breathing."); } } interface Talkable { void talk(); } class Dog extends Mammal implements Talkable { public void talk() { System.out.println("I'm a dog."); } public void doDogStuff () { System.out.println("WOOF!"); } } class Cat extends Mammal { @Override public void breathe() { System.out.println("I have kitten breath."); } }

  5. In Code C# abstract class Mammal { public virtual void breathe() { Console.WriteLine ("Generic breathing."); } } interface Talkable { void talk(); } class Dog : Mammal, Talkable { public void talk() { Console.WriteLine("I'm a dog."); } public void doDogStuff () { Console.WriteLine("WOOF!"); } } class Cat : Mammal { public override void breathe() { Console.WriteLine("I have kitten breath."); } }

  6. At this point, whats allowable? Cat c1 = new Cat(); Dog d1 = new Dog(); Dog d2 = new Cat(); // Not allowed. Why? Cat c2 = new Dog(); // Not allowed. Why? Talkable t1 = new Dog(); // Allowed Talkable t2 = new Cat(); // Not allowed. Why? Mammal m1 = new Mammal(); // Not allowed. Why? Mammal m2 = new Dog(); // Allowed Mammal m3 = new Cat(); // Allowed Cat c3 = new Mammal(); // Not allowed. Why? m2.breathe(); // Prints "Generic breathing" m3.breathe(); // Prints "I have kitten breath" m2.talk(); // Not allowed!!! ERROR. Why? // BUT m2 IS A DOG! What can we do? // Allowed // Allowed

  7. Solving the problem Checking and Casting Looking at the code, we know m2 is a Dog (right now) However, at runtime, m2 could be a Cat We should check to see what m2 is, then type cast it: if (m2 instanceof Dog) { // Java ((Dog)m2).talk(); } // Now allowed if (m2 is Dog) { ((Dog)m2).talk(); } // C#

  8. Understanding the Cast // Does the cast occur first? (Dog)m2.talk(); // Guaranteed the cast occurs first ((Dog)m2).talk(); Convince yourself: ((Dog)m2) is a Dog

  9. Pulling it all together - Java ArrayList<Mammal> animals = new ArrayList<Mammal>(); animals.add(new Dog()); animals.add(new Cat()); // m will be polymorphic - starts as a Dog but // becomes a Cat in the second pass of the loop for (Mammal m : animals) { m.breathe(); // works no matter what. Why? if (m instanceof Dog) { ((Dog)m).doDogStuff(); } if (m instanceof Talkable) { // Interfaces work too ((Talkable)m).talk(); } // if } // for

  10. Pulling it all together C# List<Mammal> animals = new List<Mammal>(); animals.Add(new Dog()); animals.Add(new Cat()); // m will be polymorphic - starts as a Dog but // becomes a Cat in the second pass of the loop foreach (Mammal m in animals) { m.breathe(); // works no matter what. Why? if (m is Dog) { ((Dog)m).doDogStuff(); } if (m is Talkable) { // Interfaces work too ((Talkable)m).talk(); } // if } // for

  11. Food for Thought All objects in the List were Mammals (not Dogs/Cats) When we pull something out of the list, it s a Mammal However, there s no such thing as a Mammal, so Check to see what type it is (using is or instanceof) Type cast it into a subclass Call methods on the casted object

  12. Summary The example was comprehensive If you understood it, you understand the big picture If not, keep asking until you do! You should now see how these concepts tie together We have inheritance, abstract classes and interfaces We can t have polymorphism without inheritance We can now have a polymorphic collection of objects

Related