Ask a question to Desklib · AI bot


Assignment on Explaining Polymorphism

Added on -2019-09-20

| 11 pages
| 1647 words

Trusted by 2+ million users,
1000+ happy students everyday

Running Case: OBJECT-ORIENTED APPLICATIONSIndividual_Assignment_4(Object-Oriented Applications)Student Name: Student ID: Course Name: Course ID:Faculty Name: University Name:
OBJECT-ORIENTED APPLICATIONS1Object Orientation.Object orientation is basically a programming paradigm that works on the concept of real world entities (Banerjee et al, 1987). It provides various concepts like classes, objects and many more which we will learn later. Object oriented programming maps the problem into real world concepts and then find the solution to it implementing the OOPs concepts and principles.In OOPs object is a real world entity that has some property and behavior where as a Class is group of objects of similar functionalities.For example Pen is a class which represents a community or caste with common functionalities i.e. to write whereas Reynolds and Parker are objects which has some different properties like name, color, shape etc. and functionalities as already said writing.There are four principles of OOPs (Booch, 2006):1.Encapsulation 2.Polymorphism3.Inheritance 4.AbstractionPolymorphism : Polymorphism as it name explains many, hence polymorphism mean many forms. In object orientation polymorphism means a way of performing one task by various ways.For example, if we need to start a bike we can do it by kick, by self-start or by pushing it. In object orientation polymorphism is of two types : 1.Static or Compile time Polymorphism : Method Overloading Method overloading is achieved is achieved by putting same name for the methods and changing its arguments. If we have to perform the same task by multiple ways we do so.For example we have to add numbers.We can do so by,public int add (int a, int b){// some code}public double add (double a, double b){// some code}public long add (long a, long b){
OBJECT-ORIENTED APPLICATIONS2// some code}This is how we can define multiple methods with same to add numbers. This is done to improve the readability of code.Method overloading can be achieved by using the same syntax of all the methods with different data types as arguments or numbers of the arguments.Method overloading is not achieved by changing the return type of the methods.Technically, it is an example of the static or compile time polymorphism because method bindingbetween the method calling and the method definition happens at complete time by the compiler only. The diagrammatic representation of method overloading is shown below : The above diagram states the example of overloading. Here the word manager is overloaded. The same manager word can be used for various roles like General Manager, Product Manager, Project Manager, etc. The word manager is overloaded here.2.Dynamic or runtime polymorphism : Method Overriding : Method overriding in java is possible by Inheritance. When a child class method defines a new functionalities for any method that is inherited from Parent class, this is known as method overriding (Holzle et al, 1991).Method overriding is example of dynamic or runtime polymorphism because method binding between method calling and method definition is done by the JVM not by the compiler when the object is created.The advantage of method overriding is code reusability, the child class method can use the parent class method or functionalities and enhance them, this increases the readability of the code too. Suppose for example,Class Animal
OBJECT-ORIENTED APPLICATIONS3{Public void eat (){// some code}}Class Dog extends Animal{Public void eat (){// some modified code}}The above diagram shows how the dog has overridden the eat () functionalities of the animal class. The dog has changed the functionality i.e. overridden. Encapsulation : Encapsulation in java is generally a technique to bind or wrap the data fields and the methods together (Kniesel & Theisen, 2001). The data binding is done so that the any data needed by outsiders can only getit through the methods. Suppose that,We have a class with some fields,Class Employee{private int id;private int salary;public void setId ( int id )

Found this document preview useful?

You are reading a preview
Upload your documents to download
Become a Desklib member to get accesss

Students who viewed this