Java - Unit-1 Chapter1
Java - Unit-1 Chapter1
OOPs Concepts
Introduction to Software and types of software's
In a computer system, the software is basically a set of instructions or commands that tells a
computer what to do. Or in other words, the software is a computer program that provides a
set of instructions to execute a user’s commands and tell the computer what to do. For
example like MS-Word, MS-Excel, PowerPoint, etc. The chart below describes the types of
software.
Above is the diagram of types of software. Now we will briefly describe each
type and its subtypes :
System Software
System software is software that directly operates the computer hardware and provides the
basic functionality to the users as well as to the other software to operate smoothly. Or in
other words, system software basically controls a computer’s internal functioning and also
controls hardware devices such as monitors, printers, and storage devices, etc. It is like an
interface between hardware and user applications, it helps them to communicate with each
other because hardware understands machine language(i.e. 1 or 0) whereas user
applications are work in human-readable languages like English, Hindi, German, etc. so
system software converts the human-readable language into machine language and vice
versa.
Features of system software:
1. System Software is closer to the computer system.
2. System Software is written in a low-level language in general.
3. System software is difficult to design and understand.
4. System software is fast in speed(working speed).
5. System software is less interactive for the users in comparison to application software.
Application Software
Software that performs special functions or provides functions that are much more than the
basic operation of the computer is known as application software. Or in other words,
application software is designed to perform a specific task for end-users. It is a product or a
program that is designed only to fulfill end-users’ requirements. It includes word
processors, spreadsheets, database management, inventory, payroll programs, etc.
Features of application software:
1. An important feature of application software is it performs more specialized tasks like
word processing, spreadsheets, email, etc.
2. Mostly, the size of the software is big, so it requires more storage space.
3. Application software is more interactive for the users, so it is easy to use and design.
4. The application software is easy to design and understand.
5. Application software is written in a high-level language in general.
Less interactive for the users More interactive for the users
to run.
13. Data There is not any proper way for There is a possibility of data
hiding data hiding. hiding.
C++ is pretty consistent between In Java, semantics differs for primitive and
8 Type semantics
primitive and object types. object types.
Access control
In C++, a flexible model with In Java, the model is cumbersome and
10 and object
constant protection is available. encourages weak encapsulation.
protection
Syntax :
C++
#include <iostream>
int main() {
return 0;
Java
import java.io.*;
class GFG {
System.out.println("GFG!");
}
Classes and objects
Object − Objects have states and behaviors. Example: A dog has states - color, name,
breed as well as behaviors – wagging the tail, barking, eating. An object is an instance
of a class.
Class − A class can be defined as a template/blueprint that describes the
behavior/state that the object of its type support.
Objects in Java
If we consider the real-world, we can find many objects around us, cars, dogs, humans, etc.
All these objects have a state and a behavior.
If we consider a dog, then its state is - name, breed, color, and the behavior is - barking,
wagging the tail, running.
If you compare the software object with a real-world object, they have very similar
characteristics.
Software objects also have a state and a behavior. A software object's state is stored in fields
and behavior is shown via methods.
So in software development, methods operate on the internal state of an object and the object-
to-object communication is done via methods.
An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen,
table, car, etc. It can be physical or logical (tangible and intangible). The example of an
intangible object is the banking system.
Creating an Object
As mentioned previously, a class provides the blueprints for objects. So basically, an object is
created from a class. In Java, the new keyword is used to create new objects.
To create an object of MyClass, specify the class name, followed by the object name, and use
the keyword new:
Example
Output :
Classes in Java
A class is a blueprint from which individual objects are created.
Example
void barking() {
}
void hungry() {
}
void sleeping() {
}
}
A class can have any number of methods to access the value of various kinds of methods. In
the above example, barking(), hungry() and sleeping() are methods.
Local variables − Variables defined inside methods, constructors or blocks are called
local variables. The variable will be declared and initialized within the method and the
variable will be destroyed when the method has completed.
Instance variables − Instance variables are variables within a class but outside any
method. These variables are initialized when the class is instantiated. Instance
variables can be accessed from inside any method, constructor or blocks of that
particular class.
Class variables − Class variables are variables declared within a class, outside any
method, with the static keyword.
Data abstraction
Abstraction in Java
Abstraction is a process of hiding the implementation details and
showing only functionality to the user.
Another way, it shows only essential things to the user and hides the
internal details, for example, sending SMS where you type the text and
send the message. You don't know the internal processing about the
message delivery.
1. Abstract class
2. Interface
running safely
Example-2
abstract class Bank
{
abstract int getRateOfInterest();
}
class SBI extends Bank{
int getRateOfInterest(){return 7;}
}
class PNB extends Bank{
int getRateOfInterest(){return 8;}
}
class TestBank{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new PNB();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
}}
Rate of Interest is: 7 %
Rate of Interest is: 8 %
Encapsulation
Encapsulation is one of the key features of object-oriented programming. Encapsulation
refers to the bundling of fields and methods inside a single class.
It prevents outer classes from accessing and changing fields and methods of a class. This also
helps to achieve data hiding.
class Area {
int length;
int breadth;
this.length = length;
this.breadth = breadth;
class Main {
rectangle.getArea();
Output -Area: 30
In the above example, we have created a class named Area. The main purpose of this class is
to calculate the area. To calculate an area, we need two variables: length and breadth and a
method: getArea(). Hence, we bundled these fields and methods inside a single class. Here,
the fields and methods can be accessed from other classes as well. Hence, this is not data
hiding.This is only encapsulation. We are just keeping similar codes together.
Encapsulation refers to the bundling of related fields and methods together. This can be used
to achieve data hiding. Encapsulation in itself is not data hiding.
Why Encapsulation?
In Java, encapsulation helps us to keep related fields and methods together, which makes our
code cleaner and easy to read.
class Person {
if (age >= 0) {
this.age = age;
Here, we are making the age variable private and applying logic inside the setAge() method.
Now, age cannot be negative.
The getter and setter methods provide read-only or write-only access to our class fields. For
example,
It helps to decouple components of a system. For example, we can encapsulate code into
multiple bundles.These decoupled components (bundle) can be developed, tested, and
debugged independently and concurrently. And, any changes in a particular component do
not have any effect on other components.We can also achieve data hiding using
encapsulation. In the above example, if we change the length and breadth variable into
private, then the access to these fields is restricted.And, they are kept hidden from outer
classes. This is called data hiding.
Data Hiding
Data hiding is a way of restricting the access of our data members by hiding the
implementation details. Encapsulation also provides a way for data hiding.We can use access
modifiers to achieve data hiding. For example,
class Person {
// private field
// getter method
return age;
// setter method
this.age = age;
class Main {
Output=My age is 24
In the above example, we have a private field age. Since it is private, it cannot be accessed
from outside the class.In order to access age, we have used public methods: getAge() and
setAge(). These methods are called getter and setter methods. Making age private allowed us
to restrict unauthorized access from outside the class. This is data hiding. If we try to access
the age field from the Main class, we will get an error.
p1.age = 24;
Inheritance in Java
The new class that is created is known as subclass (child or derived class)
and the existing class from where the child class is derived is known
as superclass (parent or base class).
The extends keyword is used to perform inheritance in Java. For example,
class Animal {
// methods and fields
}
In the above example, the Dog class is created by inheriting the methods
and fields from the Animal class.
Here, Dog is the subclass and Animal is the superclass.
Example 1:
class Animal {
class Main {
public static void main(String[] args) {
}
}
Output
My name is Rohu
I can eat
labrador.name = "Rohu";
labrador.eat();
Here, labrador is an object of Dog . However, name and eat() are the
members of the Animal class.
Since Dog inherits the field and method from Animal , we are able to access
the field and method using the object of the Dog .
Benefits of Inheritance
Inheritance promotes reusability. ...
Reusability enhanced reliability. ...
As the existing code is reused, it leads to less development and maintenance costs.
Inheritance makes the sub classes follow a standard interface.
Inheritance helps to reduce code redundancy and supports code extensibility.
Polymorphism
Polymorphism is an important concept of object-oriented programming. It
simply means more than one form.
That is, the same entity (method or operator or object) can perform different
operations in different scenarios.
Example: Java Polymorphism
class Polygon {
// renders Square
public void render() {
System.out.println("Rendering Square...");
}
}
// renders circle
public void render() {
System.out.println("Rendering Circle...");
}
}
class Main {
public static void main(String[] args) {
Output
Rendering Square...
Rendering Circle...