CHARACTERISTICS
Emphasis on data rather than procedure
Programs are divided into object
Methods that use/change object data are in the same class
Data is hidden and cannot be externally accessed
Objects can communicate through methods
New data/functions can easily be added
ADVANTAGES
Modularity – object code can be changed/maintained independently from main
code. Objects can also be easily passed around the system
Information-hiding – details of objects remain hidden outside of the object’s
methods
Code re-use – if an object already exists, it can be reused in other people’s code
Plug-ability and debugging ease - If a particular object turns out to be
problematic, you can simply remove it and replace it with a different object
CLASS
It is a construct used to define a distinct new type and is made up of:
o Attributes/fields that are grouped together to describe the object
o Methods that describe what the object can do
Class – A template or prototype to define the common attributes and behaviour
for all the objects of the class
Examples: JFrame Form & Scanner
OBJECT
Object – a dynamic instance of a class
Is a software bundle of related state and behaviour
Need to declare an object as an instance of the class (declaring doesn’t create it)
CONSTRUCTORS
Constructor – used to create an object and set up fields of an object so that it is
ready to use
You cannot call any of the class’ methods (unless the class is declared as static)
unless an object is created first
A constructor must be declared as public and must be the same as the class’
name
Default constructors:
o Does not have any parameters and is used to initialise the attributes with
default values
Parameterised constructors:
o Contains parameters which are used to assign values to the corresponding
attributes
, o Partially parameterised: only part of the class’ attributes will be
assigned
o Fully parameterised: all attributes will be assigned custom values
ENCAPSULATING PRINCIPLE
Encapsulation – a way of organizing data and functionality into a structure
(single unit) by concealing the way the object is implemented
o Allows restricted access to data
o Guarantees the integrity of the object’s data
o Visibility to the functionalities of the object, but not to its data
Encapsulation principle – the class’ attributes should only be read/written by
methods of the class itself
o Encapsulating principle is also known as information hiding
Information hiding – all fields and auxiliary methods are declared as private in
the class. Access to the data is limited to using Accessors and Mutators
data abstraction – the concept of representing important details and hiding
away the implementation details
ACCESS SPECIFIERS AND MODIFIERS
Access specifiers are used to identify access rights for the data and member
functions of the class
It helps in implementing information hiding & data abstraction
In Java, there are 3 access specifiers:
o Public – can be used by any function (“+”)
o Protected – can only be used by member functions and classes & function
of the class. They can also be used by classes derived from the class
(“#”)
o Private – can only be used by member functions and classes & functions
of the class (“-”)
DESTRUCTOR/GARBAGE COLLECTOR
Destructor – a special method called when the object’s lifecycle is over in order
to free the memory and deallocate resources
o Used in languages with manual memory management
o Important to use to avoid memory leaks
Garbage collector – program that runs on the JVM and recovers memory by
deleting objects that are not used anymore or are not accessible from the code
o Runs automatically and periodically checks the references in contrast to
the objects in the memory heap
o In Java you can access the Garbage Collector, however you cannot force
what memory will be release, just that you would like to release garbage
(System.gc() OR Runtime.getRuntime().gc() )
The difference between a Destructor and a Garbage Collector is the level of
developer’s interference to memory management
ACCESSOR/GETTER METHODS
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through EFT, credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying this summary from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller verdaa17. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy this summary for R50,00. You're not tied to anything after your purchase.