Hi guys,
In this post I’ll decide to cover two basic concepts
behind OOP. Object oriented programing concepts memorizing or hard coding is
impractical, because OOP is the pure conceptual thing. If you need to geek of
this area you should try to understand this concept rather than memorizing.
I’ll go through the OOP design pattern based on Abstract & Inheritance
concepts. Following a practical approach will be lead to better understanding
of above two concepts. Today's post I’ll organize in two different approaches,
first I’ll explain the OOP concepts with an aid of in real world scenarios and
then I’ll implement explained concept using JAVA technical aspect. Ok, let’s
start our OO journey with JAVA.
Real world
scenario -
When we
depict above diagram, every human being in the world owns above 4 properties
which is listed under person. But when we want to categorize every human being
in the whole world under person category it will be a disaster, because 7
billion humans in the world can be put into person category. In order to
introduce well organized approach as well as a resource utilization
approach, we can categorize persons into two major categories. Female &
Male, now see above approach how to lead to well-organized resource utilization
approach. I’m highlighting in the beginning of this paragraph, every human
beings own 4 properties in their life. These four properties didn’t depend on
the gender. In order to maintain well-organized approach we divide persons into
two categories, but still we listed above properties under person category. We
reuse listed properties rather than duplicating. We divide the whole world into
two categories, but still we listed common features under person category. Now
we can put the whole world into female & male category, but still we
maintain person category for listed common features of females & males.
Technical
approach –
Now I’m
instructing to how to implement the technical approach of the above real world
scenario using JAVA, This three category implementing as three different
Classes, But in order to implement this scenario, according to OO concepts, we
will use Inheritance for avoiding duplicating resources also maintain the
well-organized approach. Let’s see how we utilize resources, I’ll implement
person class and include all common properties in the person class.
Additionally, I’ll implement female & male classes for maintaining
well-organized approach.
public class person {
private String name;
private int age;
private genderType gender;
private Date birthDay;
public person(String name, int age, genderType gender, Date birthDay) {
this.name = name;
this.age = age;
this.gender = gender;
this.birthDay = birthDay;
}
}
private String name;
private int age;
private genderType gender;
private Date birthDay;
public person(String name, int age, genderType gender, Date birthDay) {
this.name = name;
this.age = age;
this.gender = gender;
this.birthDay = birthDay;
}
}
public class female extends person{
public female(String name, int age, genderType gender, Date birthDay) {
super(name, age, gender, birthDay);
}
}
public female(String name, int age, genderType gender, Date birthDay) {
super(name, age, gender, birthDay);
}
}
public class male extends person{
public male(String name, int age, genderType gender, Date birthDay) {
super(name, age, gender, birthDay);
}
}
public male(String name, int age, genderType gender, Date birthDay) {
super(name, age, gender, birthDay);
}
}
enum – If you
want to implement user defined data type like ‘genderType’, you can easily add
an enum and define data within curly brackets and using when we want. But you
can’t assign user defined data type data just like a standard data type. You
should assign values like this,
<Defined
Data type>.< Data>
Ex:
genderType.female
enum genderType{
Female,
Male
}
Female,
Male
}
Real world
scenario –
Now we will
listed what are the specified features and common features of female and male
lives. Following table explain about that. You can see the study level of each
person didn’t depend on the gender, but when we consider marital status of each
person it will be depend on gender.
Technical
approach –
When
illustrate the real world scenario, you can see a ‘Marital Status’ is depend on
the gender type, I mean marital status, we can categorize as a common feature
of both male & female. But when we using that usually both married an
unmarried male as a “Mr.” and married female as a “Mrs.” & Unmarried female
as “Miss.” So you can see these common features how to depend on gender. What I
want to do is, I will define a method in person class and, method, body defined
in both female and male classes according to changes.
public abstract String getMaritalStatus(boolean status);
public String getMaritalStatus(boolean status)
{
if(status)
return "Miss";
else
return "Mrs.";
}
{
if(status)
return "Miss";
else
return "Mrs.";
}
public String getMaritalStatus(boolean status)
{
return "Mr.";
}
{
return "Mr.";
}
Real world
scenario –
Now we will
categorize 3 persons into these three categories, I’ll put the mark these real
world data represented category. You can see each real world data can be put
into female or male category, because of this reason person category is empty.
Technical
approach –
Let’s
implement the JAVA technical approach, according to real world scenarios,
according to above table you can see any real world data can’t categorize as
person, but still common features listed under person class. What I am going to
change is, I’ll change person class as the Abstract class, my target
maintains person class as a super class and as well as a method of resource
utilization but never create any of the object of it.
public abstract class person{ //rest of the class }
Method
overriding – This is another important feature in OO programing,
But here I’ll not go into that much detail about this. I will just quickly go
through it, when we implement any of the class we can see by default methods
automatically generated for us, in the class. ToString (), compareTo (), Equals
() are the common method which we can see when we disassemble the code using
Java. We can override those methods according to our requirements. Following
example, I’ll override auto generated method inside the person class.
Additionally, when we want to override compareTo () method we need to implant
comparable interface inside the class. You can simply implement needed
interfaces after the class name as you wish.
Another thing
is, when we want to compare different properties we can’t override the same
comparator () method in the same class. We can use separate class and implement
the comparator interface in JAVA and simply override the compare () method
according to our requirements.
public abstract class person implements Comparable<person>
{
//rest of the class
@Override
public int compareTo(person t) {
if(t.name.compareTo(name) == 1)
{
return -1;
}else
{
if(t.name.compareTo(name) == -1)
{
return 1;
}else
{
return 0;
}
}
}
}
{
//rest of the class
@Override
public int compareTo(person t) {
if(t.name.compareTo(name) == 1)
{
return -1;
}else
{
if(t.name.compareTo(name) == -1)
{
return 1;
}else
{
return 0;
}
}
}
}
@Override
public String toString() {
return "person{" + "name=" + name + ", age=" + age + ", gender=" + gender + ", birthDay=" + birthDay + '}';
}
public String toString() {
return "person{" + "name=" + name + ", age=" + age + ", gender=" + gender + ", birthDay=" + birthDay + '}';
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final person other = (person) obj;
if (!Objects.equals(this.name, other.name)) {
return false;
}
return true;
}
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final person other = (person) obj;
if (!Objects.equals(this.name, other.name)) {
return false;
}
return true;
}
import java.util.Comparator;
public class compareByAge implements Comparator<person>{
@Override
public int compare(person t, person t1) {
if(t.getAge() > t1.getAge())
{
return 1;
}else
{
if(t.getAge() < t1.getAge())
{
return -1;
}else
{
return 0;
}
}
}
}
public class compareByAge implements Comparator<person>{
@Override
public int compare(person t, person t1) {
if(t.getAge() > t1.getAge())
{
return 1;
}else
{
if(t.getAge() < t1.getAge())
{
return -1;
}else
{
return 0;
}
}
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class RealWorldApplication {
public static String sortByName(List<person> personList)
{
Collections.sort(personList);
return "Sort by name";
}
public static String sortByAge(List<person> personList)
{
Collections.sort(personList,new compareByAge());
return "Sort by age";
}
public static void displayPersons(List<person> personList)
{
for(person tempPerson : personList)
{
System.out.println(tempPerson);
}
}
public static void main(String[] args) {
Scanner Scan = new Scanner(System.in);
List humanList = new ArrayList<person>();
humanList.add( new male("Saman Ranpitiya", 25, genderType.Male, null));
humanList.add( new male("Nuwan Kumara", 30, genderType.Male, null));
humanList.add( new female("Kumari Silva", 20, genderType.Female, null));
boolean status = true;
do
{
System.out.println("Enter your choise >> ");
int option = Scan.nextInt();
switch(option)
{
case 1 : displayPersons(humanList);
break;
case 2 : sortByAge(humanList);
break;
case 3 : sortByName(humanList);
break;
case 4 : status = false;
}
}while(status);
}
}
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class RealWorldApplication {
public static String sortByName(List<person> personList)
{
Collections.sort(personList);
return "Sort by name";
}
public static String sortByAge(List<person> personList)
{
Collections.sort(personList,new compareByAge());
return "Sort by age";
}
public static void displayPersons(List<person> personList)
{
for(person tempPerson : personList)
{
System.out.println(tempPerson);
}
}
public static void main(String[] args) {
Scanner Scan = new Scanner(System.in);
List humanList = new ArrayList<person>();
humanList.add( new male("Saman Ranpitiya", 25, genderType.Male, null));
humanList.add( new male("Nuwan Kumara", 30, genderType.Male, null));
humanList.add( new female("Kumari Silva", 20, genderType.Female, null));
boolean status = true;
do
{
System.out.println("Enter your choise >> ");
int option = Scan.nextInt();
switch(option)
{
case 1 : displayPersons(humanList);
break;
case 2 : sortByAge(humanList);
break;
case 3 : sortByName(humanList);
break;
case 4 : status = false;
}
}while(status);
}
}
Happy Coding.....
Regards,
Denuwan Himanga.
No comments:
Post a Comment