Table Of ContentInteractive
Object-Oriented
Programming
in Java
Learn and Test Your Programming Skills
—
Second Edition
—
Vaskaran Sarcar
Foreword by Avirup Mullick
Interactive
Object- Oriented
Programming in Java
Learn and Test Your
Programming Skills
Second Edition
Vaskaran Sarcar
Foreword by Avirup Mullick
Interactive Object-Oriented Programming in Java
Vaskaran Sarcar
Bangalore, Karnataka, India
ISBN-13 (pbk): 978-1-4842-5403-5 ISBN-13 (electronic): 978-1-4842-5404-2
https://doi.org/10.1007/978-1-4842-5404-2
Copyright © 2020 by Vaskaran Sarcar
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Celestin Suresh John
Development Editor: Mathew Moodie
Coordinating Editor: Divya Modi
Cover designed by eStudioCalamar
Cover image designed by Pixabay
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street,
6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, email orders-ny@springer-sbm.
com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner)
is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware
corporation.
For information on translations, please email rights@apress.com or visit http://www.apress.com/rights-
permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book’s product page, located at www.apress.com/978-1-4842-5403-5. For more
detailed information, please visit http://www.apress.com/source-code.
Printed on acid-free paper
Dear Reader,
You motivate me with your nice and loving comments,
you hurt me with your extremely critical comments, but in the
end you help me to become a better person and a better author.
So, this book is dedicated to you.
Table of Contents
About the Author ....................................................................................................xix
About the Technical Reviewer ................................................................................xxi
Acknowledgments ................................................................................................xxiii
Preface ..................................................................................................................xxv
Who Is This Book For? ..........................................................................................xxix
Guidelines for Using This Book .............................................................................xxxi
Conventions Used in This Book...........................................................................xxxiii
Foreword .............................................................................................................xxxv
Part I: Fundamentals of Object- Oriented Programming .................................1
Chapter 1: Object-Oriented Programming Concepts ................................................3
Class and Objects �������������������������������������������������������������������������������������������������������������������������5
Encapsulation �������������������������������������������������������������������������������������������������������������������������������6
Abstraction ������������������������������������������������������������������������������������������������������������������������������������6
Inheritance ������������������������������������������������������������������������������������������������������������������������������������7
Polymorphism �������������������������������������������������������������������������������������������������������������������������������7
Q&A Session ����������������������������������������������������������������������������������������������������������������������������������8
Summary���������������������������������������������������������������������������������������������������������������������������������������9
Chapter 2: The Building Blocks: Class and Objects ................................................11
Class �������������������������������������������������������������������������������������������������������������������������������������������11
Object ������������������������������������������������������������������������������������������������������������������������������������������11
Constructor ���������������������������������������������������������������������������������������������������������������������������������14
Demonstration 1 ��������������������������������������������������������������������������������������������������������������������17
Q&A Session ��������������������������������������������������������������������������������������������������������������������������19
v
Table of ConTenTs
Demonstration 2 ��������������������������������������������������������������������������������������������������������������������19
Analysis ���������������������������������������������������������������������������������������������������������������������������������20
Demonstration 3 ��������������������������������������������������������������������������������������������������������������������26
Demonstration 4 ��������������������������������������������������������������������������������������������������������������������29
Demonstration 5 ��������������������������������������������������������������������������������������������������������������������31
Passing Variable-Length Arguments to Methods ������������������������������������������������������������������������32
Demonstration 6 ��������������������������������������������������������������������������������������������������������������������32
Analysis ���������������������������������������������������������������������������������������������������������������������������������33
Q&A Session ��������������������������������������������������������������������������������������������������������������������������33
Summary�������������������������������������������������������������������������������������������������������������������������������������35
Chapter 3: Classes and Objects in Depth ................................................................37
Static Variables and Methods������������������������������������������������������������������������������������������������������37
Demonstration 1 ��������������������������������������������������������������������������������������������������������������������38
Q&A Session ��������������������������������������������������������������������������������������������������������������������������39
Access Control ����������������������������������������������������������������������������������������������������������������������������39
Demonstration 2 ��������������������������������������������������������������������������������������������������������������������40
Getter-Setter Methods�����������������������������������������������������������������������������������������������������������������42
Demonstration 3 ��������������������������������������������������������������������������������������������������������������������42
Q&A Session ��������������������������������������������������������������������������������������������������������������������������43
Initialization Block �����������������������������������������������������������������������������������������������������������������������43
Demonstration 4 ��������������������������������������������������������������������������������������������������������������������44
Q&A Session ��������������������������������������������������������������������������������������������������������������������������45
Nested Class �������������������������������������������������������������������������������������������������������������������������������47
Demonstration 5 ��������������������������������������������������������������������������������������������������������������������47
Q&A Session ��������������������������������������������������������������������������������������������������������������������������49
Copying an Object �����������������������������������������������������������������������������������������������������������������������49
Using Copy Constructors �������������������������������������������������������������������������������������������������������49
Demonstration 6 ��������������������������������������������������������������������������������������������������������������������50
Q&A Session ��������������������������������������������������������������������������������������������������������������������������51
Wrapper Class �����������������������������������������������������������������������������������������������������������������������������51
Demonstration 7 ��������������������������������������������������������������������������������������������������������������������52
vi
Table of ConTenTs
Garbage Collection ����������������������������������������������������������������������������������������������������������������������54
Q&A Session ��������������������������������������������������������������������������������������������������������������������������55
Demonstration 8 ��������������������������������������������������������������������������������������������������������������������57
Finalization ����������������������������������������������������������������������������������������������������������������������������������60
Demonstration 9 ��������������������������������������������������������������������������������������������������������������������61
Summary�������������������������������������������������������������������������������������������������������������������������������������63
Chapter 4: The Concept of Inheritance ...................................................................65
Types of Inheritance ��������������������������������������������������������������������������������������������������������������������65
Single Inheritance������������������������������������������������������������������������������������������������������������������65
Hierarchical Inheritance ��������������������������������������������������������������������������������������������������������66
Multi-level Inheritance�����������������������������������������������������������������������������������������������������������67
Multiple Inheritance ���������������������������������������������������������������������������������������������������������������68
Demonstration 1 ��������������������������������������������������������������������������������������������������������������������69
Q&A Session ��������������������������������������������������������������������������������������������������������������������������70
Demonstration 2 ��������������������������������������������������������������������������������������������������������������������70
Q&A Session ��������������������������������������������������������������������������������������������������������������������������72
Demonstration 3 ��������������������������������������������������������������������������������������������������������������������72
Q&A Session ��������������������������������������������������������������������������������������������������������������������������74
Demonstration 4 ��������������������������������������������������������������������������������������������������������������������75
Q&A Session ��������������������������������������������������������������������������������������������������������������������������76
A Special Keyword: super �����������������������������������������������������������������������������������������������������������76
Demonstration 5 ��������������������������������������������������������������������������������������������������������������������77
Demonstration 6 ��������������������������������������������������������������������������������������������������������������������81
Q&A Session ��������������������������������������������������������������������������������������������������������������������������83
Demonstration 7 ��������������������������������������������������������������������������������������������������������������������84
Q&A Session ��������������������������������������������������������������������������������������������������������������������������85
Demonstration 8 ��������������������������������������������������������������������������������������������������������������������86
Q&A Session ��������������������������������������������������������������������������������������������������������������������������87
Demonstration 9 ��������������������������������������������������������������������������������������������������������������������88
Q&A Session ��������������������������������������������������������������������������������������������������������������������������89
Summary�������������������������������������������������������������������������������������������������������������������������������������90
vii
Table of ConTenTs
Chapter 5: Get Familiar with Polymorphism ...........................................................91
Method Overloading ��������������������������������������������������������������������������������������������������������������������91
Demonstration 1 ��������������������������������������������������������������������������������������������������������������������91
Q&A Session ��������������������������������������������������������������������������������������������������������������������������93
Demonstration 2 ��������������������������������������������������������������������������������������������������������������������95
Q&A Session ��������������������������������������������������������������������������������������������������������������������������96
Demonstration 3 ��������������������������������������������������������������������������������������������������������������������96
Q&A Session ��������������������������������������������������������������������������������������������������������������������������97
Demonstration 4 ��������������������������������������������������������������������������������������������������������������������97
Q&A Session ��������������������������������������������������������������������������������������������������������������������������98
Demonstration 5 ��������������������������������������������������������������������������������������������������������������������99
Method Overriding ��������������������������������������������������������������������������������������������������������������������101
Demonstration 6 ������������������������������������������������������������������������������������������������������������������102
Q&A Session ������������������������������������������������������������������������������������������������������������������������104
Demonstration 7 ������������������������������������������������������������������������������������������������������������������106
Runtime Polymorphism �������������������������������������������������������������������������������������������������������������108
Demonstration 8 ������������������������������������������������������������������������������������������������������������������109
Q&A Session ������������������������������������������������������������������������������������������������������������������������111
Demonstration 9 ������������������������������������������������������������������������������������������������������������������111
Q&A Session ������������������������������������������������������������������������������������������������������������������������114
Demonstration 10 ����������������������������������������������������������������������������������������������������������������115
Using the final Keyword ������������������������������������������������������������������������������������������������������������117
Blank final Variables ������������������������������������������������������������������������������������������������������������119
Q&A Session ������������������������������������������������������������������������������������������������������������������������120
Demonstration 11 ����������������������������������������������������������������������������������������������������������������120
Q&A Session ������������������������������������������������������������������������������������������������������������������������122
Demonstration 12 ����������������������������������������������������������������������������������������������������������������124
Q&A Session ������������������������������������������������������������������������������������������������������������������������126
Demonstration 13 ����������������������������������������������������������������������������������������������������������������127
Q&A Session ������������������������������������������������������������������������������������������������������������������������128
Demonstration 14 ����������������������������������������������������������������������������������������������������������������129
viii
Table of ConTenTs
Covariant Return Type ���������������������������������������������������������������������������������������������������������������130
Demonstration 15 ����������������������������������������������������������������������������������������������������������������130
Demonstration 16 ����������������������������������������������������������������������������������������������������������������133
Q&A Session ������������������������������������������������������������������������������������������������������������������������134
Use of Private Constructors ������������������������������������������������������������������������������������������������������135
Q&A Session ������������������������������������������������������������������������������������������������������������������������135
Demonstration 17 ����������������������������������������������������������������������������������������������������������������136
Q&A Session ������������������������������������������������������������������������������������������������������������������������138
Summary�����������������������������������������������������������������������������������������������������������������������������������139
Chapter 6: Abstract Classes and Interfaces: The True Art in OOP ........................141
Abstract Classes �����������������������������������������������������������������������������������������������������������������������141
Demonstration 1 ������������������������������������������������������������������������������������������������������������������142
Demonstration 2 ������������������������������������������������������������������������������������������������������������������143
Q&A Session ������������������������������������������������������������������������������������������������������������������������146
Interfaces ����������������������������������������������������������������������������������������������������������������������������������151
Demonstration 3 ������������������������������������������������������������������������������������������������������������������153
Q&A Session ������������������������������������������������������������������������������������������������������������������������154
Demonstration 4 ������������������������������������������������������������������������������������������������������������������155
Q&A Session ������������������������������������������������������������������������������������������������������������������������156
Demonstration 5 ������������������������������������������������������������������������������������������������������������������157
Q&A Session ������������������������������������������������������������������������������������������������������������������������158
Demonstration 6 ������������������������������������������������������������������������������������������������������������������158
Q&A Session ������������������������������������������������������������������������������������������������������������������������160
Demonstration 7 ������������������������������������������������������������������������������������������������������������������160
Q&A Session ������������������������������������������������������������������������������������������������������������������������163
Marker Interface �����������������������������������������������������������������������������������������������������������������������163
A Quick Tour with Annotations ��������������������������������������������������������������������������������������������������164
Demonstration 8 ������������������������������������������������������������������������������������������������������������������165
Demonstration 9 ������������������������������������������������������������������������������������������������������������������169
Javadoc Snapshots��������������������������������������������������������������������������������������������������������������170
Q&A Session ������������������������������������������������������������������������������������������������������������������������171
ix
Table of ConTenTs
Default Methods in Interfaces ���������������������������������������������������������������������������������������������������173
Demonstration 10 ����������������������������������������������������������������������������������������������������������������173
Q&A Session ������������������������������������������������������������������������������������������������������������������������175
Demonstration 11 ����������������������������������������������������������������������������������������������������������������176
Q&A Session ������������������������������������������������������������������������������������������������������������������������178
Demonstration 12 ����������������������������������������������������������������������������������������������������������������179
Summary�����������������������������������������������������������������������������������������������������������������������������������180
Chapter 7: Packages .............................................................................................183
Creating a Package �������������������������������������������������������������������������������������������������������������������185
Demonstration 1 ������������������������������������������������������������������������������������������������������������������188
Key Notes About Packages in Java ��������������������������������������������������������������������������������������192
Q&A Session ������������������������������������������������������������������������������������������������������������������������193
Demonstration 2 ������������������������������������������������������������������������������������������������������������������195
Q&A Session ������������������������������������������������������������������������������������������������������������������������196
Troubleshooting Common Errors in Command-line Environment ���������������������������������������������197
Summary�����������������������������������������������������������������������������������������������������������������������������������200
Chapter 8: Understanding Class Variables and Class Methods ............................201
Class Variables and Class Methods �������������������������������������������������������������������������������������������202
Demonstration 1 ������������������������������������������������������������������������������������������������������������������202
Working with Nested Classes ���������������������������������������������������������������������������������������������������203
Demonstration 2 ������������������������������������������������������������������������������������������������������������������203
Q&A Session ������������������������������������������������������������������������������������������������������������������������207
Initialization Blocks Versus Constructors ����������������������������������������������������������������������������������208
Demonstration 3 ������������������������������������������������������������������������������������������������������������������210
Method Hiding Versus Method Overriding ���������������������������������������������������������������������������������214
Demonstration 4 ������������������������������������������������������������������������������������������������������������������215
Q&A Session ������������������������������������������������������������������������������������������������������������������������217
Method Overloading ������������������������������������������������������������������������������������������������������������������217
Demonstration 5 ������������������������������������������������������������������������������������������������������������������217
Q&A Session ������������������������������������������������������������������������������������������������������������������������220
x