Unit 3: Java as OOP - Overview
Fundamentals of JAVA, Arrays: one dimensional array, multi-dimensional array, alternative array
declaration statements, String Handling: String class methods, Classes and Methods: class fundamentals,
declaring objects, assigning object reference variables, adding methods to a class, returning a value,
constructors, this keyword, garbage collection, finalize() method, overloading methods,
argument passing, object as parameter, returning objects, access control, static, final,
nested and inner classes, command line arguments, variable - length arguments.
Arrays
* Array is collection of data usually of homogenous type.
* Array can be visualised as multiple units of data type combined together.
* Array indexing usually starts from 0 and varies till n-1 where n is the size of array.
* Arrays are usually of two types:
-> one dimensional array
# It grows in a single direction.
# The syntax for declaring 1D array in Java is:
# The length of an array can be found using length method.
# A visual representation of 1d array is as follows:
-> multidimensional arrays
# It is another type of array which grows in different directions.
# for example, a 2d array grows in 2 directions or can store elements in 2 directions.
# There are different types of multidimensional arrays like:
> 2d arrays
> 3d arrays, etc.
# The syntax for declaring a multidimensional is as follows:
# Usually the elements in a 2d array can be accessed by following syntax:
where a and b are the indices and have numerical value.
Arrays
A 2d array can be visualised in the form of a matrix:
Strings
* A string in java is a group of characters.
* Strings are helpful in increasing code readability.
* Strings can be initialised by two methods:
-> Using character array
# Create a character array.
# Convert it into string using by creating a new string object.
-> Using string object
# Create a string object of a java.lang.String class.
# There are two ways to create a string object
> Using new keyword
> Using string literal
String Class Methods
* There are different string class methods:
-> Java string toUpperCase()
# It converts the string into uppercase.
-> Java string toLowerCase()
# It converts the string into lowercase
-> Java string concat()
# It combines two strings.
-> Java string equals()
# It checks if the two strings are equal both by value and their case.
-> Java string indexOf()
# Finds the index of a specific character in the string
Java classes and objects
* Java programming language is the purest OOP based language which involves
classes and objects.
* Class is a type of user defined data type.
* It is collection of items which share common attributes and characteristics.
* Properties:
-> Java class has no real world existence
-> They do not occupy any memory.
* Syntax to create a class
* A simple example of class is
Java classes and objects
* Java Objects
-> It represents a real life entity
-> It is the representation of a class.
-> When we create a class object, we assign it a memory.
-> Syntax for creating a class object is:
-> An example of java object is:
Adding methods to class
* Adding a method or behaviour to a class is very simple;
Java Constructor
* A constructor is a special type of function used to initialise objects.
* It has following properties:
-> It has same name as the class.
-> It doesn't have a return type
-> They are called when we declare a object using a new keyword.
* Syntax of a constructor
* A simple example of constructor
Types of constructor
* There are different types of constructor:
-> Default Constructor
# A constructor that has no parameters
# It is usually called for every object creation.
-> Parameterised Constructor
# A constructor with parameters allows you to initialize instance variables
with specific values when an object is created.
Garbage Collection
* Garbage collection is one of the best features of Java.
* It is a best way of memory management.
* It helps in preventing memory leaks and ensures efficient memory usage.
* The steps included in garbage collection are:
-> Object creation
# When you create an object using new keyword, memory is allocated in the
heap.
-> Object reference
# Reference variables are used to refer to that object.
# When an object is no longer referenced by any variable, it becomes eligible
for garbage collection.
-> Mark and Sweep
-> Finalisation
# before the garbage is collected, the 'finalise()' method is called.
# It performs the last rituals before the object is destroyed.
Garbage Collection
* Garbage collection is one of the best features of Java.
* It is a best way of memory management.
* It helps in preventing memory leaks and ensures efficient memory usage.
* The steps included in garbage collection are:
-> Object creation
# When you create an object using new keyword, memory is allocated in the
heap.
-> Object reference
# Reference variables are used to refer to that object.
# When an object is no longer referenced by any variable, it becomes eligible
for garbage collection.
-> Mark and Sweep
-> Finalisation
# before the garbage is collected, the 'finalise()' method is called.
# It performs the last rituals before the object is destroyed.
Finalise() method
* Called by the garbage collector on an object when garbage collection determines
that there are no more references to the object.
* It's a way for an object to clean up resources before it gets destroyed.
* It is deprecated now
* The exact time when the finalize() method will be called is not guaranteed.
It's also possible that it might not be called at all if the program ends before
garbage collection occurs.
Function Overloading
* Method overloading in Java means having two or more methods (or functions) in
a class with the same name and different arguments (or parameters).
* It can be with a different number of arguments or different data types of arguments.
* For example
* Method overloading in Java achieves compile-time polymorphism, also known
as static polymorphism.
* Polymorphism is an OOPs concept in which an entity can take multiple forms.
Access Control
* In java, class can control the access of their data members and methods by using
access specifiers.
* Access specifiers control the access to data feilds, methods and classes.
* There are actually 4 types of access modifiers:
-> public
-> private
-> protected
-> default ( package private)
* Public access modifier
-> Using public access modifier, data members and methods can be accessed
by other classes and packages.
* Private access modifier
-> Using private access modifier, data members and methods can be accessed
within the class but not by other class and packages
* Protected access modifier
-> Using protected access modifier, data members and methods can be accessed
within the class and within the package but not outside the package.
* default access modifier
-> Using access modifier, data members and methods can be accessed within the
class and within the package but not outside the package.
Static keyword