Question and Answer: Java Technologies

Only available on StudyMode
  • Download(s) : 22
  • Published : February 18, 2013
Open Document
Text Preview
Q. 1: a)What is Object Oriented Paradigm? Explain advantages of Object Oriented Programming. 5 marks
Ans Object Oriented paradigm: The Object Oriented paradigm is centered on the concept of the object. Everything is focused on objects. In this language, program consists of two things: first, a set of objects and second the way they interact with each other. Computation in this paradigm is viewed as the simulation of real world entities. The popular programming languages in this paradigm are C++, Simula, Smalltalk and Java

Object Oriented programming: The world is Object Oriented, and Object Oriented programming expresses programs in the ways that model how people perceive the world. Figure 2 shows different real world objects around us which we often use for performing different functions. This shows that problem solving using the objects oriented approach is very close to our real life problem solving techniques. The basic difference in Object Oriented programming (OOP) is that the program is organized around the data being operated upon rather than the operations performed. The basic idea behind OOP is to combine both, data and its functions that operate on the data into a single unit called object. Object Oriented methods are favored because many experts agree that Object Oriented techniques are more disciplined than conventional structured techniques. (Martin and Odell 1992) b) What is polymorphism? Explain the advantages of polymorphismwith an example. Solution : Polymorphism is the capability of a method to do different things based on the object through which it is invoked or object it is acting upon. For example method find _area will work definitely for Circle object and Triangle object In Java, the type of actual object always determines method calls; object reference type doesn't play any role in it. You have already used two types of polymorphism (overloading and overriding) in the previous unit and in the current unit of this block. Now we will look at the third: dynamic method binding. Java uses Dynamic Method Dispatch mechanism to decide at run time which overridden function will be invoked. Dynamic Method Dispatch mechanism is important because it is used to implement runtime polymorphism in Java. Java uses the principle: "a super class object can refer to a subclass object" to resolve calls to overridden methods at run time. If a superclass has method that is overridden by its subclasses, then the different versions of the overridden methods are invoked or executed with the help of a superclass reference variable. Assume that three subclasses (Cricket_Player Hockey_Player and Football_Player) that derive from Player abstract class are defined with each subclass having its own Play() method. abstract class Player // class is abstract

{
private String name;
public Player(String nm)
{
name=nm;
}
public String getName() // regular method
{
return (name);
}
public abstract void Play();
// abstract method: no implementation
}
class Cricket_Player extends Player
{
Cricket_Player( String var)
{
}
public void Play()
{
System.out.println("Play Cricket:"+getName());
}
}
class Hockey_Player extends Player
{
Hockey_Player( String var)
{
}
public void Play()
{
System.out.println("Play Hockey:"+getName());
}
}
class Football_Player extends Player
{
Football_Player( String var)
{
}
public void Play()
{
System.out.println("Play Football:"+getName());
}
}
public class PolyDemo
{
public static void main(String[] args)
{
Player ref; // set up var for an Playerl
Cricket_Player aCplayer = new Cricket_Player("Sachin"); // makes specific objects Hockey_Player aHplayer = new Hockey_Player("Dhanaraj");
Football_Player aFplayer = new Football_Player("Bhutia");
// now reference each as an Animal
ref = aCplayer;
ref.Play();
ref = aHplayer;
ref.Play();
ref = aFplayer;
ref.Play();
}
}
Output: Play Cricket:Sachin
Play Hockey:Dhanaraj
Play Football:Bhutia
Question 2: a)...
tracking img