0% found this document useful (0 votes)
47 views3 pages

Lab 19 Manual CSE215 Summer 2024

Uploaded by

Tamanna Adiba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views3 pages

Lab 19 Manual CSE215 Summer 2024

Uploaded by

Tamanna Adiba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

‭North South University‬

‭Department of Electrical and Computer Engineering‬


‭CSE 215L (Programming Language II Lab)‬
‭Lab 19: Introduction to Abstract Classes‬

‭Instructor: Tanvir Yeasin Opy‬

‭Objective:‬
‭●‬ ‭Get introduced to Abstract Classes‬
‭Concept of Abstraction:‬
‭The‬‭concept‬‭of‬‭abstraction‬‭deals‬‭with‬‭hiding‬‭the‬‭internal‬‭implementation‬‭details‬‭and‬‭showing‬‭the‬‭functionality‬‭to‬
t‭he user.‬
‭ onsider‬ ‭a‬ ‭coffee‬ ‭machine‬ ‭where‬ ‭you‬ ‭make‬ ‭coffee,‬ ‭and‬‭we‬‭do‬‭not‬‭know‬‭about‬‭the‬‭internal‬‭functioning‬‭of‬‭the‬
C
‭coffee‬‭machine.‬‭But,‬‭the‬‭coffee‬‭machine‬‭has‬‭functions‬‭that‬‭we‬‭need‬‭to‬‭know‬‭only;‬‭nothing‬‭else‬‭matters.‬‭All‬‭we‬
‭need‬‭to‬‭know‬‭is‬‭what‬‭button‬‭does‬‭what‬‭function;‬‭we‬‭don’t‬‭need‬‭to‬‭know‬‭where‬‭the‬‭button‬‭is‬‭connected‬‭to,‬‭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:‬
‭ or‬‭so‬‭long,‬‭we‬‭had‬‭created‬‭the‬‭classes‬‭with‬‭the‬‭required‬‭data‬‭fields,‬‭constructors,‬‭and‬‭methods‬‭and‬‭then‬‭could‬
F
‭write‬‭what‬‭each‬‭method‬‭or‬‭constructor‬‭should‬‭do,‬‭meaning‬‭that‬‭we‬‭could‬‭dictate‬‭the‬‭implementations.‬‭We‬‭could‬
‭also create instances of classes as well in the old days.‬
‭However,‬‭a‬‭class‬‭that‬‭is‬‭declared‬‭to‬‭be‬‭abstract‬‭is‬‭called‬‭an‬‭abstract‬‭class‬‭.‬‭We‬‭have‬‭to‬‭use‬‭the‬‭abstract‬‭keyword‬
t‭o 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 in‬‭Italics‬‭.‬
‭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‬ ‭have‬‭a‬‭body‬‭and‬‭must‬‭end‬‭with‬‭a‬
‭semi-colon.‬ ‭In‬ ‭addition,‬ ‭they‬ ‭must‬ ‭be‬ ‭in‬ ‭an‬ ‭abstract‬ ‭class‬ ‭and‬ ‭MUST‬ ‭be‬ ‭overridden,‬ ‭which‬ ‭means‬ ‭that‬ ‭we‬
‭SHOULD‬ ‭NOT‬ ‭use‬ ‭the‬‭final‬‭or‬‭static‬‭keywords‬‭in‬‭those‬‭methods‬‭as‬‭using‬‭the‬‭final‬‭keyword‬‭prevents‬‭us‬‭from‬
‭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:‬


‭ ‬ ‭Create‬‭an‬‭ArrayList‬‭of‬‭Books‬‭in‬‭the‬‭Main‬‭Method;‬‭the‬‭ArrayList‬‭must‬‭contain‬‭a‬‭combination‬‭of‬‭Fiction‬‭and‬
‭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.‬

You might also like