Lab 19 Manual CSE215 Summer 2024
Lab 19 Manual CSE215 Summer 2024
Objective:
● Get introduced to Abstract Classes
Concept of Abstraction:
Theconceptofabstractiondealswithhidingtheinternalimplementationdetailsandshowingthefunctionalityto
the user.
onsider a coffee machine where you make coffee, andwedonotknowabouttheinternalfunctioningofthe
C
coffeemachine.But,thecoffeemachinehasfunctionsthatweneedtoknowonly;nothingelsematters.Allwe
needtoknowiswhatbuttondoeswhatfunction;wedon’tneedtoknowwherethebuttonisconnectedto,which
part of the machine, or which wire the button uses.
We can achieve abstraction by using abstract classes and interfaces.
What is an Abstract Class:
orsolong,wehadcreatedtheclasseswiththerequireddatafields,constructors,andmethodsandthencould
F
writewhateachmethodorconstructorshoulddo,meaningthatwecoulddictatetheimplementations.Wecould
also create instances of classes as well in the old days.
However,aclassthatisdeclaredtobeabstractiscalledanabstractclass.Wehavetousetheabstractkeyword
to declare a class to be abstract. However, we have to know the following about the abstract classes:
● I t can have abstract or non-abstract methods.
● We cannot create an instance of the abstract class
● Abstract classes MUST be inherited by any other class.
● Abstract classes can also have their data fields and constructors.
● In the UML Diagram, these classes are represented inItalics.
What is an Abstract Method:
bstract methods are those methods that are declared as abstract and do not have implementation. Like the
A
abstract classes, these are also in italics in the UML diagram. They do not haveabodyandmustendwitha
semi-colon. In addition, they must be in an abstract class and MUST be overridden, which means that we
SHOULD NOT use thefinalorstatickeywordsinthosemethodsasusingthefinalkeywordpreventsusfrom
MAKING ANY ADJUSTMENTS, which includes overriding.
Notes:
I n the UML Diagram, you will notice that the abstract class and methods are indicated in italics.
●
● For FictionBook
displayInfo() prints the name, author, and genre in successive lines.
o
o isRecommendedForAge() returns true if the age is 14 and above.
● For NonFictionBook
o displayInfo() prints the name, author, and subject in successive lines.
o isRecommendedForAge() returns true if the age is 18 and above.
Tasks to be done:
◻
Implement the Book, FictionBook, and NonFictionBook classes and declare the required data fields,
constructors, and methods accordingly.
◻
Create an instance of FictionBook and NonFictionBook class, and test their displayInfo() and
isRecommendedForAge() methods; the value passed for the latter parameter should be user-input for now.
Homework:
Carry forward the Classes we have made in the Classwork
Tasks to be done:
◻
CreateanArrayListofBooksintheMainMethod;theArrayListmustcontainacombinationofFictionand
Non-Fiction Books.
◻
Add 3 Fiction books and 3 Non-Fiction Books into the ArrayList.
◻
Iterate through the ArrayList and display information about each book, then print whether each book is
recommended for someone who is 16 years old or not.