Java Object-Oriented Programming Tutorial on Abstraction & Equals Method with Stone River eLearning
In our last blog, we discussed about inheritance and how inheritance adds another dimension to the Object Oriented nature of JAVA. In this section, we will discuss another feature of JAVA programming that makes this language so popular and useful for the development of applications. Taking our discussion of inheritance to the next level, it is important that we make ourselves aware of the functionality of abstract classes and methods in JAVA. Abstract classes and methods find great utility in applications where the same variables or methods need to be used in different ways.
What is an abstract method?
The general definition of an abstract method is; a method which is declared without an implementation. However; a more apt version of this definition would be; an abstract method is a method that can be implemented in different ways in different sections of a JAVA program. An abstract method can also be left blank depending upon the requirement of the program.
What is an abstract class?
Building on the definition that we gave for abstract method, a class needs to be defined as abstract if it includes an abstract method as its member. When an abstract class is inherited, all the abstract methods in it need to be implemented again in the child class. However; if the child class is also an abstract class, then the implementation of abstract methods of parent class can be neglected.
Eg:
public abstract class shape { int x,y; int area1; public abstract void area( ); } public class square extends shape { public void area() { x=5; y=x; area1 = x*y; System.out.println(“Area of the given shape is: “ +area1); }} public class rectangle extends shape { public void area() { x=5; y=6; area1= x*y; System.out.println(“Area of the given shape is: “ +area1); }} public class Test { public static void main(String[] args) { square s = new square(); rectangle r = new rectangle(); s.area(); r.area(); } }
In the above example, the abstract method “area” is included inside the abstract class “shape”
. Once the shape class is inherited by square and rectangle class respectively, the abstract method “area” is re-implemented in each of the child classes to perform specific functionalities.
Note:
- Abstract keyword is used to define a class or a method as an abstract class or abstract method respectively.
- The keyword “abstract” must be included between the class or method name and the public access modifier.
- Abstract method must be redefined in the child class.
Equals() method
As we did in one of our previous blog posts, we will discuss another built-in method of JAVA programming that finds extensive application in JAVA codes. The “equals()” method is of great utility to compare the contents of two strings or any other class.
Eg:
public class Hello { public void show() { string s1 = “Hello”; string s2 = “Hello”; if (s1.equals(s2)) { System.out.println(“The two strings are same”); } else { System.out.println(“The two strings are different”); }}} public class Test { public static void main(String[] args) { Hello h1 = new Hello(); h1.show(); } }
In the above example, “equals()” method is used to compare the contents inside the two strings objects, s1 and s2. As in the case with “toString()” method, the “equals()” method can also be overridden to include a modified operation for it.
Note:
- If (s1==s2) was used instead of (s1.equals(s2)), objects s1 and s2 would have been compared and not the contents inside these string objects.