Java: Object Oriented Programming Concepts - Polymorphism | ChargeAhead LLC | Skillshare

Java: Object Oriented Programming Concepts - Polymorphism

ChargeAhead LLC, Technologist

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
8 Lessons (12m)
    • 1. Introduction

    • 2. Roadmap

    • 3. Audience

    • 4. Prerequisites

    • 5. Tools

    • 6. Concept

    • 7. Demo

    • 8. Summary


About This Class

Java is an Object Oriented programming (OOP) language. Polymorphism is a very important OOP concept, which allows and action to be performed multiple ways based on the object it is invoked on. In this class we will understand this important concept.


1. Introduction: object oriented programming or Opie is a very powerful programming paradigm used where many popular languages like Java. We have been covering the O P concepts in a series, of course. Java already 200 concepts. Abstraction, Joe. Our object oriented concepts encapsulation Java object oriented concepts, inheritance. Please force those. If you want to get more details in this course, you will understand polymorphism. 2. Roadmap: in this course will start off talking about the audience for the scores, prerequisites required and tools needed to follow along. Next, it will conceptually understand polymorphism. Finally, we will have a demo toe makeover concepts concrete. All right, this is going to be exciting. Course, let's get started. 3. Audience: course is for programmers trying to understand the fundamental object oriented programming concepts, which can be applied to many more off the 200 programming languages like by Tone C sharp, etcetera. Off course, the scores is useful for someone starting to learn Java. 4. Prerequisites: This is a beginner level course and they're not really any prerequisites for the scores. I will explain everything and should be pretty simple for you to follow along. 5. Tools: I will be using Spring Tool Suite for the demos. However, if we're more comfortable with it and want to use some other ideas like intelligent extra, please feel free to do so. These other programs can alter Britain. But just using a note pad, however, An I D, provides many nice features like intelligence, compile time check, ease of running, etcetera. So it's always good to get familiar with the 90. I would be off course using Java or open Judy Kay for the demonstrations. I will be using Open Judy Kaye, 11. However, these concepts should work with even lower versions like Julie. Get 10 etcetera. Using the latest version of Java is always recommended, though you can find the court for the demo in the scores at the following get WRL. 6. Concept: Polly means many and morph means forms, so polymorphism means many forms. In real lives, we see examples of polymorphism. A lady plays multiple roles, rule off a mother of wife of friend, a sister, etcetera. She reacts differently based on the role she is playing in Java, Polymorphism means to perform a single action in multiple ways. There are two types off polymorphism static or compile time polymorphism, in which we create all loaded methods with the same name, but for different parameters. And at compile time, the compiler decides which meant her to call based on the parameters past runtime polymorphism is when at runtime the Java virtual machine figures or the matter to call based on the type of object it is being invoked on, Polymorphism requires inheritance or interface implementations, that is, understand the two types of polymorphism with examples and demos. Just as in real lives, we work with various objects at cars, tables, chairs, etcetera with solve problems. Similarly, in object oriented programming with think in terms of objects to solve problems, let's take the animal object. It has attributes like color, weight and methods like speak, eat, let's create another eat metal when we are giving a treat to the animal for doing something good. The regular eat method is when they are eating regular food, so we have to eat methods. Let's see it. In terms of a Java class, here is a job class for an animal where it has attributes for color weight. Ah, constructor to initialize the color and weight a speak may 3rd and eat method without arguments in San, which we had a sprinting I'm eating food here is another matured with the same name eat but it has a parameter of type string called Treat Inside were printing. I'm eating a treat. So when we call, eat with no parameters. The first metalist card when we call eat with a parameter the second metal is called. This is static polymorphism. We're calling the same matter. Name will invoke different actions based on parameters. This is a resort at compile time. Hence the name compiled time polymorphism. Now we have covered inheritance in my earlier video, Java object oriented concepts and headed ins. Let's say we have two classes dog and cat which inherit from the animal class, so both extend the animal class and then headed all the attributes and methods of the period. Animal flus, not a giant last, can override the periods, mentor with the over at an audition and provide their own implementation. So the dark class has its implementation off the speak mattered in which we printed the console. I'm a dog, the cat class, or are the two and prince of the screen I am a cat now in our tester, we have a variable animal as new animal assigned to the type animal. Next we have available of type dog as new dog up, dusted to and assigned to the animal type. Finally, we have the cat type as new cat assigned to the super class type animal calling animal Lord speak will resolve it. Run time to the animal classes. Speak myth heard Don't not speak the resolve Toe I am a dog and cannot speak will resolve the can't speak mattered. I'm a cat So even though they're all assigned to the super class variable time the speak metal implementation is called off. The appropriate subclass based on the variable it is acting upon. This is a runtime polymorphism. So the advantage of polymorphism is to decouple decline class from the implementation code in sort of being hard quartered declined. Class receives the appropriate implementation to execute the necessary action. Does it provides loose coupling? Let's see it in action. 7. Demo: hair. I'm running spring tools, feet of flavor if eclipse. But you can download and use any I d you want. You can use no penalty to write Java programs, but a 90 makes it very easy. So let me click New Java Project. Let's give this project and name Polymorphism Project Click Finish. Now here is the project. Expanding it. We see this source for her. That's right. Click and choose a new class. Given the name Animal click finish, let's create attributes string, color and double wait. Let's create a constructor to initialize this class as public animal, which takes in string, color and double weight and inside a science. These to the instance variables using this, which is a key word to reference the instance. Variable. Let's assign this start color to color and this don't wait to wait. Let's create a public method. Speak, which returns void or nothing inside. Let the sprint hello from animal. Let's create a neat matter inside which we print to the console. I am eating food. Let's create another eat Mattered, which takes in a string parameter treat and inside were to print. I'm meeting Treat and concoction ate the treat name in the speak matter. Also, let's concoct innate using the plus sign color, plus my readers plus wait, let's see static polymorphism in action. First, let's right. Click and choose new class. Call it tester. Inside, let's create a public static void. Mean string arks. Now this is the method which the jobber and time would call it creates a mean mentored within the Java application. It's a core method of the program, and it calls all others. It can't be done values and accepts parameters for complex command line processing. Inside, let's initialize a variable of type animal and create an instance offered by calling new animal passing in the color of gray and a rate of 20. Now let's call animal Lord eat first without any parameter, and then call animal, not eat with a parameter cookie. Let's run this by right clicking and choosing run as Java application. The first speak call with no argument, calls the first matter, and then the call with the argument calls the second method. The method to call is resort at compile time due to the parameters, so it is called static or compile time polymorphism. Now let's create a subclass which will inherit from this class. Let's right, click and choose new class and call it dog insight. Using extent. Let's indicate that headers from animal it inherits both the color and we'd and tribute of the animal class as well as the speak and to eat methods. Let's create its constructor so public dog, which takes in color and weight and then it involves the constructor of apparent using the super keyword passing in color and weight. Let's using the act or, at a notation, create the dark glasses. Unique implementation on the speak Mentor inside which we print, I am a dog. Next, let's similarly create the card class by right clicking and choosing new class and call it card inside using extents. Let's indicate that it inherits from animal again. Let's create its constructor similar to the dark class and invoke using super the constructor of the periods animal class. Let's create the card specific implementation off the speak mattered by all reading the one from the period by using the art of war at an audition and calling public void speak an inside print. I am a cat. Let's go to the tester and create a variable of type animal. Call it dog and initialize it, using the dogs constructor as new dog, passing in a color of black and a weight off 30. Similarly, let's create another variable as new cat with color white and wait 20 and assign it to super class variable type animal. Now let's call animal dot Speak dog not speak and canned. Don't speak. Let's run this by right clicking and choosing run as Java application. Even though all three variables are the clearers off type animal, they call the appropriate surpluses implementation off the speak method. Based on how they were in Stan, she hated or created Java. Runtime sees the type of class and then at runtime calls the right implementation. This is a runtime polymorphism. 8. Summary: in this course. We talked about the important concept off polymorphism, which means multiple forms. It means to perform a single action in multiple ways. We saw that there are two types of polymorphism static or compile time polymorphism, in which we create overloaded methods for the same name, but for different parameters. And at compile time, the compiler decides which matter to call based on the parameters past runtime polymorphism is when and run time. The Java virtual machine figures out Demeter to call based on the type of object it's being invoked on. Hopefully you got a good understanding off the important concept off following dwarfism. Thanks for watching.