Oops
Oops
Object oriented design started right from the moment computers were invented. Programming was
there, and programming approaches came into the picture. Programming is basically giving certain
instructions to the computer.
At the beginning of the computing era, programming was usually limited to machine language
programming. Machine language means those sets of instructions that are specific to a particular
machine or processor, which are in the form of 0‟s and 1‟s. These are sequences of bits (0100110…).
But it‟s quite difficult to write a program or develop software in machine language.
It‟s actually impossible to develop software used in today‟s scenarios with sequences of bits. This was
the main reason programmers moved on to the next generation of programming languages,
developing assembly languages, which were near enough to the English language to easily
understand. These assembly languages were used in microprocessors. With the invention of the
microprocessor, assembly languages flourished and ruled over the industry, but it was not enough.
Again, programmers came up with something new, i.e., structured and procedural programming.
Structured Programming –
The basic principle of the structured programming approach is to divide a program into functions and
modules. The use of modules and functions makes the program more understandable and readable. It
helps to write cleaner code and to maintain control over the functions and modules. This approach
gives importance to functions rather than data. It focuses on the development of large software
applications, for example, C was used for modern operating system development. The programming
languages: PASCAL (introduced by Niklaus Wirth) and C (introduced by Dennis Ritchie) follow this
approach.
1. Physical Level:
It describes how the records are stored, which are often hidden from the user. It can be described with
the phrase, “block of storage.”
2. Logical Level:
It describes data stored in the database and the relationships between the data. The programmers
generally work at this level as they are aware of the functions needed to maintain the relationships
between the data.
3. View Level:
Application programs hide details of data types and information for security purposes. This level is
generally implemented with the help of GUI, and details that are meant for the user are shown.
Encapsulation –
Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes
the idea of wrapping data and the methods that work on data within one unit, e.g., a class in Java. This
concept is often used to hide the internal state representation of an object from the outside.
Inheritance –
Inheritance is the ability of one class to inherit capabilities or properties of another class, called the
parent class. When we write a class, we inherit properties from other classes. So when we create a
class, we do not need to write all the properties and functions again and again, as these can be
inherited from another class which possesses it. Inheritance allows the user to reuse the code
whenever possible and reduce its redundancy.
Polymorphism –
Polymorphism is the ability of data to be processed in more than one form. It allows the performance
of the same task in various ways. It consists of method overloading and method overriding, i.e., writing
the method once and performing a number of tasks using the same method name.
Access Modifiers in C++
Access modifiers are used to implement an important feature of Object-Oriented Programming known
as Data Hiding. Consider a real-life example:
The Indian secret informatic system having 10 senior members have some top secret regarding
national security. So we can think that 10 people as class data members or member functions who
can directly access secret information from each other but anyone can‟t access this information other
than these 10 members i.e. outside people can‟t access information directly without having any
privileges. This is what data hiding is.
Access Modifiers or Access Specifiers in a class are used to set the accessibility of the class
members. That is, it sets some restrictions on the class members not to get directly accessed by the
outside functions.
There are 3 types of access modifiers available in C++:
1. Public
2. Private
3. Protected
Advantages of OOPs –
It models the real world very well.
With OOP, programs are easy to understand and maintain.
OOP offers code reusability. Already created classes can be reused without having to
write them again.
OOP facilitates the quick development of programs where parallel development of
classes is possible.
With OOP, programs are easier to test, manage and debug.
Disadvantages of OOP –
With OOP, classes sometimes tend be over-generalised.
The relations among classes become superficial at times.
The OOP design is tricky and requires appropriate knowledge. Also, one needs to do
proper planning and design for OOP programming.
To program with OOP, the programmer needs proper skills such as that of design,
programming and thinking in terms of objects and classes etc.
Packages In Java
Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces.
Packages are used for:
Preventing naming conflicts. For example there can be two classes with name
Employee in two packages, college.staff.cse.Employee and college.staff.ee.Employee
Making searching/locating and usage of classes, interfaces, enumerations and
annotations easier
Providing controlled access: protected and default have package level access control.
A protected member is accessible by classes in the same package and its subclasses. A default
member (without any access specifier) is accessible by classes in the same package only.
Packages can be considered as data encapsulation (or data-hiding).
All we need to do is put related classes into packages. After that, we can simply write an import class
from existing packages and use it in our program. A package is a container of a group of related
classes where some of the classes are accessible are exposed and others are kept for internal
purpose.
We can reuse existing classes from the packages as many time as we need it in our program.
How packages work?
Package names and directory structure are closely related. For example if a package name
is college.staff.cse, then there are three directories, college, staff and cse such that cse is present
in staff and staff is present college. Also, the directory college is accessible
through CLASSPATH variable, i.e., path of parent directory of college is present in CLASSPATH. The
idea is to make sure that classes are easy to locate.
Package naming conventions : Packages are named in reverse order of domain names, i.e.,
org.geeksforgeeks.practice. For example, in a college, the recommended convention is
college.tech.cse, college.tech.ee, college.art.history, etc.
Adding a class to a Package : We can add more classes to a created package by using package
name at the top of the program and saving it in the package directory. We need a new java file to
define a public class, otherwise we can add the new class to an existing .java file and recompile it.
Subpackages: Packages that are inside another package are the subpackages. These are not
imported by default, they have to imported explicitly. Also, members of a subpackage have no access
privileges, i.e., they are considered as different package for protected and default access specifiers.
Types of packages:
Built-in Packages
These packages consist of a large number of classes which are a part of Java API.Some of the
commonly used built-in packages are:
1) java.lang: Contains language support classes(e.g classed which defines primitive data types, math
operations). This package is automatically imported.
2) java.io: Contains classed for supporting input / output operations.
3) java.util: Contains utility classes which implement data structures like Linked List, Dictionary and
support ; for Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components for graphical user interfaces (like
button , ;menus etc).
6) java.net: Contain classes for supporting networking operations.
User-defined packages
These are the packages that are defined by the user. First we create a directory myPackage (name
should be same as the name of the package). Then create the MyClass inside the directory with the
first statement being the package names.
Important points:
1. Every class is part of some package.
2. If no package is specified, the classes in the file goes into a special unnamed
package (the same unnamed package for all files).
3. All classes/interfaces in a file are part of the same package. Multiple files can
specify the same package name.
4. If package name is specified, the file must be in a subdirectory called name (i.e.,
the directory name must match the package name).
5. We can access public classes in another (named) package using: package-
name.class-name
Exceptions in Java
What is an Exception?
An exception is an unwanted or unexpected event, which occurs during the execution of a program i.e
at run time, that disrupts the normal flow of the program‟s instructions.
Error vs Exception
Error: An Error indicates serious problem that a reasonable application should not try to catch.
Exception: Exception indicates conditions that a reasonable application might try to catch.
Exception Hierarchy
All exception and errors types are sub classes of class Throwable, which is base class of
hierarchy.One branch is headed by Exception. This class is used for exceptional conditions that user
programs should catch. NullPointerException is an example of such an exception.Another
branch,Error are used by the Java run-time system(JVM) to indicate errors having to do with the run-
time environment itself(JRE). StackOverflowError is an example of such an error.
Points to remember :
In a method, there can be more than one statements that might throw exception, So put
all these statements within its own try block and provide separate exception handler within
own catch block for each of them.
If an exception occurs within the try block, that exception is handled by the exception
handler associated with it. To associate exception handler, we must put catch block after it. There can
be more than one exception handlers. Each catch block is a exception handler that handles the
exception of the type indicated by its argument. The argument, ExceptionType declares the type of the
exception that it can handle and must be the name of the class that inherits from Throwable class.
For each try block there can be zero or more catch blocks, but only one finally block.
The finally block is optional.It always gets executed whether an exception occurred in
try block or not . If exception occurs, then it will be executed after try and catch blocks. And if
exception does not occur then it will be executed after the try block. The finally block in java is used to
put important codes such as clean up code e.g. closing the file or closing the connection.
Multithreading in Java
Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for
maximum utilization of CPU. Each part of such program is called a thread. So, threads are light-weight
processes within a process.
We create a class that extends the java.lang.Thread class. This class overrides the run() method
available in the Thread class. A thread begins its life inside run() method. We create an object of our
new class and call start() method to start the execution of a thread. Start() invokes the run() method on
the Thread object.
Thread Class vs Runnable Interface
1. If we extend the Thread class, our class cannot extend any other class because Java doesn‟t support
multiple inheritance. But, if we implement the Runnable interface, our class can still extend other base classes.
2. We can achieve basic functionality of a thread by extending Thread class because it provides some inbuilt
methods like yield(), interrupt() etc. that are not available in Runnable interface.