Thursday, 4 February 2016

Java Tutorial : Java Object Class(equals method override -book)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=7_s5ZAEU_2E&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java Object Class(equals method override -book)
Java Tutorial : Java Object Class(equals method override -book)
Book.java
public class Book
{
    private String name;
    private String isbnNumber;

    public Book(String name, String isbnNumber)
    {
        super();
        this.name = name;
        this.isbnNumber = isbnNumber;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getIsbnNumber()
    {
        return isbnNumber;
    }

    public void setIsbnNumber(String isbnNumber)
    {
        this.isbnNumber = isbnNumber;
    }

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((isbnNumber == null) ? 0 : isbnNumber.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Book other = (Book) obj;
        if (isbnNumber == null)
        {
            if (other.isbnNumber != null)
                return false;
        }
        else if (!isbnNumber.equals(other.isbnNumber))
            return false;
        return true;
    }
}
ObjectClassTest.java
public class ObjectClassTest
{

    public static void main(String[] args)
    {
        Book childrenStoryBook = new Book("Children Story",
                "2000");
        Book KidStoryBook = new Book("Kids Story", "2000");
        
        System.out.println("childrenStoryBook.hashCode = "+childrenStoryBook.hashCode());
        System.out.println("KidStoryBook.hashCode = "+KidStoryBook.hashCode());

        /*
         * This program displays objects are equal even
         * though childrenStoryBook and KidStoryBook
         * reference two distinct objects. They are
         * considered equal because the objects compared
         * contain the same ISBN number.
         */
        if (childrenStoryBook.equals(KidStoryBook))
        {
            System.out.println("objects are equal");
        }
        else
        {
            System.out.println("objects are not equal");
        }

    }

}
Output
childrenStoryBook.hashCode = 1537245
KidStoryBook.hashCode = 1537245
objects are equal
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/ObjectClassDemo_equals_override_book_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ObjectClassDemo_equals_override_book_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/f82b5a337c881ba48c0d1163c2b3b206ee915253/BasicJava/ObjectClassDemo_equals_override_book_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment