KEMBAR78
Unit2 - Method Overriding and Object Class | PDF | Method (Computer Programming) | Class (Computer Programming)
0% found this document useful (0 votes)
14 views6 pages

Unit2 - Method Overriding and Object Class

Uploaded by

aryan01shetty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Unit2 - Method Overriding and Object Class

Uploaded by

aryan01shetty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT 1

METHOD OVERRIDING

Overriding refers to the introduction of an instance method in a subclass that has the same
name, type signature and return type of a method in the superclass.
The implementation of the method in the subclass replaces the implementation of the method
in the superclass.

Example of method overriding

Creating a parent class

1. class Vehicle{
2. //defining a method
3. void run()
4. {
5. System.out.println("Vehicle is running");}
6. }

Creating a child class

7. class Bike2 extends Vehicle{


8. //defining the same method as in the parent class
9. void run()
10. {
11. System.out.println("Bike is running safely");
12. }
13. Public static void main(String args[]){
14. Bike2 obj = new Bike2();//creating object
15. obj.run();//calling method
16. }
17. }

Output:

Bike is running safely


1. class Bank{
2. int getRateOfInterest(){return0;}
3. }

Creating child classes

4. Class SBI extends Bank{


5. Int getRateOfInterest(){return 8;}
6. }

7. Class ICICI extends Bank{


8. Int getRateOfInterest(){return 7;}
9. }

10. Class AXIS extends Bank{


11. Int getRateOfInterest(){return 9;}
12. }

Test class to create objects and call the methods

13. class Test2{


14. public static void main(String args[]){
15. SBI s=new SBI();
16. ICICI i=new ICICI();
17. AXIS a=new AXIS();
18. System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
19. System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
20. System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
21. }
22. }

Output:

SBI Rate of Interest: 8


ICICI Rate of Interest: 7
AXIS Rate of Interest: 9
OBJECT CLASS

1. toString() Method

The java.lang.Object.toString() method returns a string representation of the object. In


general, the toString method returns a string that "textually represents" this object. The
result should be a concise but informative representation that is easy for a person to read. It
is recommended that all subclasses override this method.

The following example shows the usage of java.lang.Object.toString() method.

public class Person {


private String firstName;
private String lastName;

public String getFirstName() {


returnfirstName;
}

public void setFirstName(String firstName) {


this.firstName = firstName;
}

public String getLastName() {


return lastName;
}

public void set LastName(String lastName) {


this.lastName = lastName;
}

@Override
public String toString() {
return "Person [firstName=" + firstName + ", lastName=" + lastName + "]";
}

public static void main(String[] args) {


Person person = new Person();
person.setFirstName("Ram");
person.setLastName("Naik");
System.out.println(person.toString());
}
}

OUTPUT:
Person [firstName=Ram, lastName=Naik]

2. equals() Method

If you want to compare the references of two objects, use equals() method of Object class. This
method compares the references of two objects.

It returns true if the object invoking equals() method is equal to the object passed as an
argument to the equals() method, otherwise returns false.

The general signature of this method is as:

public boolean equals(Object obj)


This method evaluates whether two objects are equal. The syntax for calling it is:

object1.equals(object2);

The default implementation of the equals() method in the Object class is as:

public boolean equals(Object obj) {


return (this == obj);
}
This implementation tests whether two reference variables points to the same object using
double equal operator (==). We can override this method in a user defined class to check
whether two distinct objects have the same content.

For example, the equals() method of String class is inherited from the Object class and is
modified in String class to check whether two strings are identical in content.

Example Program: equals() Method of Object class

Let’s create a Java program to compare two objects by using equals() method of the Object
class. This method normally compares the references of two objects. If both references point to
the identical objects, then it gives true, otherwise it gives false.

But in the case of String objects and wrapper class objects (Character, Integer, Long, Float, etc.
are wrapper classes), it compares the content of the objects. If the contents are identical, then
it gives true. Otherwise, it gives false.

In this example program, we are using equals() method to compare two objects of a user
defined class named “Myclass”. We will also compare two objects of wrapper class “Integer”.
In case of Myclass objects, if the references are identical, it returns true, else, returns false. In
case of Integer class objects, equals() method returns true if contents are the same, otherwise
false.

Here, equals() method takes Myclass to store an int value.


public class Myclass {
int x;
Myclass(int x) {
this.x = x;
}
}

public class Compare {


public static void main(String[] args)
{
Create two Myclass objects having the same content.

// In this case, references of both objects will be different.


Myclass obj1 = new Myclass(20);
Myclass obj2 = new Myclass(20);

// Checking for equality of objects.


if(obj1.equals(obj2)) {
System.out.println("obj1 and obj2 are same");
} else {
System.out.println("obj1 and obj2 are not same");
}

// Create two wrapper class objects and store the same content.
// In this case, references of both objects will be different.
Integer obj3 = new Integer(20);
Integer obj4 = new Integer(20);

// Checking for equality of contents.


if(obj3.equals(obj4)) {
System.out.println("obj3 and obj4 are same");
} else {
System.out.println("obj3 and obj4 are not same");
}
}
}

Output:
obj1 and obj2 are not same
obj3 and obj4 are same

You might also like