Wednesday, 7 September 2016

IS-A VS HAS-A VS USE-A

Relationship in java:
In java application development, we have to provide coding part in optimistic way that less memory consumption and with less execution time,……
In java applications, to provide coding part in optimistic way we have define relationship between entities.
There are tree of relationship in java
1.       HAS-A relationship
2.       IS-A relationship
3.       USES-A relationship

Q) What is the difference between HAS-A relationship and IS-A relationship ?
Ans):-
HAS-A relationship will provide Association between entity classes in order to improve data navigation between entities and  communication between entities.

IS-A relationship will provide inheritance relation between entities classes in order to improve code reusability .


USES-A Relationship:-
It is a relationship between entity classes, where one entity will be used up to a particular behavior in another entity.
To achieve USES-A relationship between entity classes, we have to pass one entity class reference variable to another entity class method as parameter.
Ex code :-   

class Account
{
                String accNo;
                String accName;
                String accType;
                int bal;
                Account(String accNo,String accName,String accType,int bal)
                {
                                this.accNo=accNo;
                                this.accName=accName;
                                this.accType=accType;
                                this.bal= bal;
                }
}
class Transaction
{
                public void withdraw(Account acc,int wd_Amt)
                {
                               
                                acc.bal=acc.bal-wd_Amt;
                                System.out.println("Transaction Details");
                                System.out.println("================================");
                                System.out.println("Account Number     : "+acc.accNo);
                                System.out.println("Account Holder Name: "+acc.accName);
                                System.out.println("Account Type       : "+acc.accType);
                                System.out.println("Withdraw Amoutn    : "+wd_Amt);
                                System.out.println("Total Balance      : "+acc.bal);
                                System.out.println("Transaction Status:SUCCESS");
                                System.out.println("******ThanQ,Visit Again********");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                Account acc= new Account("ABC123","Sai","Saving",20000);
                                Transaction tx= new Transaction();
                                tx.withdraw(acc,1000);
                }
}
Output:-
Transaction Details
================================
Account Number     :ABC123
Account Holder Name:Sai
Account Type       :Saving
Withdraw Amoutn    :1000
Total Balance      :19000
Transaction Status:SUCCESS
******ThanQ,Visit Again********


No comments:

Post a Comment