Overriding in Java class Assignment Help

Assignment Help: >> Classes in Java >> Overriding in Java class

Overriding in Java class:

One issue we have not discussed is what happens if a subclass rede?nes instance variables or methods that exist in its superclass. The redefining of instance variables is called hiding and the rede?ning of methods is called overriding.

We do not typically want to hide variables by redefining them in a subclass, because it is best to keep the way data is stored uniform, as far as possible, and to avoid duplication of data. On the other hand, it is common that you want to use the same name for a method in a subclass as the superclass, but have the behaviour be different in the subclass. There is no reason why two objects have to respond in the same way to a method invocation. This is referred to as overriding.As an example, suppose that both our class User and its subclass PrivilegedUser have methods called getUserID with the same signatures. The superclass method returns the value of the userID, but the PrivilegedUser class method returns only a fixed string message: 

public class User

{

private String userID;

public String getUserID ()

{

return userID;

}

public void setUserID (String id)

{

userID = id;

}

}

public class PrivilegedUser extends User
{

public String getUserID ()

{

return "name is secret";

}

}

In this case, because the subclass method uses the same signature as the public superclass method, the subclass method is said to override the superclass method. If we wrote the code: 

public class TestUser
{
public static void main (String []args)
{
// has User and PrivilegedUser references
User u = new User ();
PrivilegedUser p = new PrivilegedUser();
u.setUserID("Emma");

p.setUserID("Kate");

System.out.println(u.getUserID());

System.out.println(p.getUserID());

}

}

Then we can rely on the fact that the output is:

Emma 

name is secret

The method that is invoked is determined by the type of the object it is invoked on and not by the type of the reference. The references u and p are referencing User and PrivilegedUser objects respectively, so the first invocation of getUserID() results in the execution of code defined in the User class, while the second invocation results in execution of code defined in the PrivilegedUser class. This feature allows us to redefine the effect that certain methods have in a subclass, to suit the subclass. It also ensures that if we accidentally override a superclass method, the subclass behaviour will occur if the referenced object is of the subclass type. In other words, we always get the behaviour we intend for the object. Here is another example of a situation in which we might want to reuse the same method name in subclass and superclass, but provide different behaviour. Suppose we have a greeting method:

public class Mother

{

private String name;

public String greeting ()

{

return "Pleased to meet you";

}

}

public class Child extends Mother
{
public String greeting ()
{
return "How's it going?";

}

}

You can see that it is quite reasonable that superclass and subclass have different behaviour for the same method, as it is quite natural for each to respond in their own way to invocation of the greeting method. It is useful that the two classes Mother and Child above 'speak the same language' in the sense that objects of these classes can have the same methods invoked on them. This is one way in which object-oriented programs achieve reuse of code. If it were necessary to have different names for the greeting method in Mother and Child, our code would have to provide different pathways for dealing with these kinds of objects. 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Overriding in Java class homework and assignments? Live Overriding in Java class experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Overriding in Java class homework help, java assignment help and Overriding in Java class projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd