Classes and Objects

A class is a definition for a specific organization of data and instructions (called methods) for manipulating the data.

An object is an instantiation of the class, that is, an allocation of computer memory to hold specific data.

Example --- class Book

A book is a sequentially-numbered set of pages bound together along one edge. A book has an author, a title, a publisher and an IsbnNumber.

A book can be printed, copied, and searched for specific information.


class Book

    data:  pageSet;
           title;
           author;
           publisher;
           isbn;

   methods: getTitle;
            getAuthor; 
            getIsbn;
            getPublisher
            searchKey;

Book Object
_________________________________________________

Once a book is defined and instantiated, it is accessed by sending messages to and from it. A message is the name of a method together with any data needed to make the method work.


       book myBook;

       myBook= new book();

       myBook.searchKey("whale")

_________________________________________________________

A Java program consists of a set of classes, together with a message that starts it running.

Example --- Library Management



  class Library

   data: set of Book;
         library_name;
         
   methods:
         find_book(title);
         sort_by_title;
         sort_by_author;

  _________________________

  class Myprogram

   data: Library

   methods:  start;   
             options_menu;                           if ask_for_title selected call ask_for_title;
             ask_for_title(title);                            ....
             ask_for_author(author);
             ...                                     if quit selected call quit;
             quit;

___________________________________________________________

send message: Myprogram.start