Do you know what is the greatest advantage of using OOPs language during mobile app development?
If Code Reusability is on top of your list then we all are sailing in the same boat. Today, we would be learning more about the ways we can reuse the code.
Know more: Fix [pii_email_07e5245661e6869f8bb4] Error Code
Basically, we have two techniques to reuse the code. They are as follows-
- IS-A relationship
- HAS-A relationship
Now let us learn more about them in detail with the help of an example.
IS-A Vs HAS-A Relation in Inheritance
1. IS-A Relationship
In OOPs, its complete idea is thoroughly based on inheritance. There are two types of inheritance, Class inheritance or Interface inheritance.
Let us simplify it further.
It is simply a way of saying ‘A is a B type of thing.’ For example- Employee IS-A Human, Student IS-A Human, Teacher IS-A Human. Inheritance is unidirectional. One can easily identify an IS-A relationship.
Tip- When one sees an extended keyword in a class declaration, it is known to be an IS-A relationship.
Let us consider an example:
class Human
{
// wakup() method
// sleep() method
}
class Teacher extends Human
{
// teach() method
}
class Student extends Human
{
// study() method
}
class Employee extends Human
{
// work() method
}
2. HAS-A Relationship
Here we can witness the use of instance variables that simply are references to other objects. For example, Building HAS-A Room, or Company HAS-A Developer, or School HAS-A Class, etc.
Know more: Top Node.js Trends that Every Developer should Explore
Let us consider an example:
class Writer
{
String writerName;
int age;
String place;
// Author class constructor
Writer(String writerName, int age, String place)
{
this.writerName = name;
this.age = age;
this.place = place;
}
}
class Book
{
String name;
int price;
Writer writer;
Book(String name, int price, Writer writer)
{
this.name = name;
this.price = price;
this.writer = writer;
}
public static void main(String[] args) {
Writer writer = new Writer(“Peter”, 42, “Mumbai”);
Book b = new Book(“Java for Begginer”, 800, writer);
System.out.println(“Book Name: “+b.name);
System.out.println(“Book Price: “+b.price);
System.out.println(“writer info”);
System.out.println(“Writer Name: “+b.writer.writerName);
System.out.println(“Writer Age: “+b.writer.age);
System.out.println(“Writer place: “+b.writer.place);
}
}
Conclusion
Now you know the difference between IS-A and HAS-A.
Coding is easy, especially if one has all its basics clear. For more information on any specific mobile app development related issue, feel free to reach out to us in the comment section. Our professional developers would love to help you guys out!
Read more: Android Vs. iOS
Do not forget to share your views in the comment section. We would love to know more about your opinions. Until the next update from the tech world, stay tuned to this space for more information!
Leave a comment