Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Java - Unit-1 Chapter1

Hsisnnsn

Uploaded by

jagan83411
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Java - Unit-1 Chapter1

Hsisnnsn

Uploaded by

jagan83411
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Unit-1

OOPS CONCEPTS AND JAVA FUNDAMENTALS:

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.

Types of system software:


1. Operating System: It is the main program of a computer system. When the computer
system ON it is the first software that loads into the computer’s memory. Basically, it
manages all the resources such as memory, CPU, printer, hard disk, etc., and provides
an interface to the user, which helps the user to interact with the computer system. It
also provides various services to other computer software. Examples of operating
systems are Linux, Apple macOS, Microsoft Windows, etc.
2. Language Processor: As we know that system software converts the human-readable
language into a machine language and vice versa. So, the conversion is done by the
language processor. It converts programs written in high-level programming languages
like Java, C, C++, Python, etc(known as source code), into sets of instructions that
are easily readable by machines(known as object code or machine code).
3. Device Driver: A device driver is a program or software that controls a device and
helps that device to perform its functions. Every device like a printer, mouse, modem,
etc. needs a driver to connect with the computer system eternally. So, when you connect
a new device with your computer system, first you need to install the driver of that
device so that your operating system knows how to control or manage that device.

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.

Types of application software:


There are different types of application software and those are:
1. General Purpose Software: This type of application software is used for a variety of
tasks and it is not limited to performing a specific task only. For example, MS-Word,
MS-Excel, PowerPoint, etc.
2. Customized Software: This type of application software is used or designed to perform
specific tasks or functions or designed for specific organizations. For example, railway
reservation system, airline reservation system, invoice management system, etc.
3. Utility Software: This type of application software is used to support the computer
infrastructure. It is designed to analyze, configure, optimize and maintains the system,
and take care of its requirements as well. For example, antivirus, disk fragmenter,
memory tester, disk repair, disk cleaners, registry cleaners, disk space analyzer, etc .

Difference between system software and application software

System Software Application Software

It is designed to manage the resources of It is designed to fulfill the requirements


the computer system, like memory and of the user for performing specific
process management, etc. tasks.

Written in a low-level language Written in a high-level language

Less interactive for the users More interactive for the users

Application software is not so important


System software plays vital role for the
for the functioning of the system, as it is
effective functioning of a system.
task specific.

It is independent of the application software It needs system software to run.


System Software Application Software

to run.

Procedural and object-oriented programming paradigm


Both Procedural Programming and Object Oriented Programming are high-level languages in
programming world and are widely used in the development of applications. On the basis of
nature of developing the code, both languages have different approaches on basis of which
both are differentiate from each other.

What is Procedural Programming?


Procedural Programming is a programming language that follows a step-by-step approach
to break down a task into a collection of variables and routines (or subroutines) through a
sequence of instructions. In procedural oriented programming, each step is executed in a
systematic manner so that the computer can understand what to do.
The programming model of the procedural oriented programming is derived from structural
programming. The concept followed in the procedural oriented programming is called the
"procedure". These procedures consist several computational steps that are carried out during
the execution of a program. Examples of procedural oriented programming language include
– C, Pascal, ALGOL, COBOL, BASIC, etc.
What is Object Oriented Programming?
Object-oriented Programming is a programming language that uses classes and objects to
create models based on the real world environment. These objects contain data in the form of
attributes and program codes in the form of methods or functions. In OOP, the computer
programs are designed by using the concept of objects that can interact with the real world
entities.
We have several types of object oriented programming languages, but the most popular is
one among all is class-based language. In the class-based OOP languages, the objects are the
instances of the classes that determine their types. Examples of some object oriented
programming languages are – Jave, C++, C#, Python, PHP, Swift, etc.

S.no. On the Procedural Programming Object-oriented


basis of programming

1. Definition It is a programming language Object-oriented programming


that is derived from structure is a computer programming
programming and based upon design philosophy or
the concept of calling methodology that organizes/
procedures. It follows a step-by- models software design
step approach in order to break around data or objects rather
down a task into a set of than functions and logic.
variables and routines via a
sequence of instructions.

2. Security It is less secure than OOPs. Data hiding is possible in


object-oriented programming
due to abstraction. So, it is
more secure than procedural
programming.

3. Approach It follows a top-down approach. It follows a bottom-up


approach.

4. Data In procedural programming, In OOP, objects can move


movement data moves freely within the and communicate with each
system from one function to other via member functions.
another.

5. Orientatio It is structure/procedure- It is object-oriented.


n oriented.

6. Access There are no access modifiers in The access modifiers in OOP


modifiers procedural programming. are named as private, public,
and protected.

7. Inheritanc Procedural programming does There is a feature of


e not have the concept of inheritance in object-oriented
inheritance. programming.

8. Code There is no code reusability It offers code reusability by


reusability present in procedural using the feature of
programming. inheritance.

9. Overloadi Overloading is not possible in In OOP, there is a concept of


ng procedural programming. function overloading and
operator overloading.

10. Importanc It gives importance to functions It gives importance to data


e over data. over functions.

11. Virtual In procedural programming, In OOP, there is an


class there are no virtual classes. appearance of virtual classes
in inheritance.

12. Complex It is not appropriate for complex It is appropriate for complex


problems problems. problems.

13. Data There is not any proper way for There is a possibility of data
hiding data hiding. hiding.

14. Program In Procedural programming, a In OOP, a program is divided


division program is divided into small into small parts that are
programs that are referred to as referred to as objects.
functions.

15. Examples Examples of Procedural The examples of object-


programming include C, Fortran, oriented programming are -
Pascal, and VB. .NET, C#, Python, Java,
VB.NET, and C++.

Differences between C++ and java


What is the C++?
The C++ programming language is derived from C, and it contains the properties and features
of this language. C++ was mainly created for the development of systems and applications. It
was C++ language that first brought in concepts such as classes and objects. It is capable of
encapsulating both low-level and high-level features of C, while it is considered as an
intermediate-level language as well.

What is the Java?


Java is programming language developed by Sun Microsystems. A small software
development team developed this language. They were known as the Green Team, who
initiated Java language in the year 1991. Java is built using a virtual machine, which allows
developers to maintain the security of the code and make it highly portable. Java is integrated
with a comprehensive library that offers support for abstraction. This language is specifically
created to program applications. Moreover, Java’s interpreting functionality assists in printing
systems, which has led to the development of network computing.
COMPARISON
INDEX PARAMETER C++ JAVA

C++ was developed by Bjarne


Developed / Stroustrup at Bell Labs in 1979. It Java was developed by James Gosling at Sun
1
Founded by was developed as an extension of Microsystems. Now, it is owned by Oracle.
the C language.

It has support for both procedural


Programming Java has support only for object-oriented
2 programming and object-oriented
model programming models.
programming.

C++ is platform dependent. It is


Platform Java is platform-independent. It is based on
3 based on the concept of Write
dependence the concept of Write Once Run Anywhere.
Once Compile Anywhere.

C++ supports features like


Java does not support features like operator
Features operator overloading, Goto
4 overloading, Goto statements, structures,
supported statements, structures, pointers,
pointers, unions, etc.
unions, etc.

Compilation and C ++ is only compiled and cannot


5 Java can be both compiled and interpreted.
Interpretation be interpreted.

Java, on the other hand, has more diverse


C++ has very limited libraries with
libraries with a lot of support for code
Library and Code low-level functionalities. C++
6 reusability. In Java, only calls through the
reusability support allows direct calls to native system
Java Native Interface and recently Java
libraries.
Native Access are allowed.

Memory In C++, memory management is In Java, memory management is System


7
Management manual. controlled.

C++ is pretty consistent between In Java, semantics differs for primitive and
8 Type semantics
primitive and object types. object types.

In C++, both global and


9 Global Scope Java has no support for global scope.
namespace scopes are supported.

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>

using namespace std;

int main() {

cout << "GFG!";

return 0;

 Java

/*package whatever //do not write package name here */

import java.io.*;

class GFG {

public static void main (String[] args) {

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.

What is an object in Java

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.

An object has three characteristics:

 State: represents the data (value) of an object.


 Behavior: represents the behavior (functionality) of an object such as deposit,
withdraw, etc.
 Identity: An object identity is typically implemented via a unique ID. The value of
the ID is not visible to the external user. However, it is used internally by the JVM to
identify each object uniquely.

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.

There are three steps when creating an object from a class −

 Declaration − A variable declaration with a variable name with an object type.


 Instantiation − The 'new' keyword is used to create the object.
 Initialization − The 'new' keyword is followed by a call to a constructor. This call
initializes the new object.

To create an object of MyClass, specify the class name, followed by the object name, and use
the keyword new:

Example

Create an object called "obj" and print the value of x:

public class MyClass {


int x = 5;

public static void main(String[] args) {


MyClass Obj = new MyClass();
System.out.println(Obj.x);
}
}

Output :

 Classes in Java
A class is a blueprint from which individual objects are created.

Following is a sample of a class.

Example

public class Dog {


String breed;
int age;
String color;

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.

A class can contain any of the following variable types.

 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.

Ways to achieve Abstraction

There are two ways to achieve abstraction in java

1. Abstract class
2. Interface

Abstract class in Java


A class which is declared as abstract is known as an abstract class. It
can have abstract and non-abstract methods. It needs to be extended and
its method implemented. It cannot be instantiated.

Example of abstract class

abstract class A{}


Abstract Method in Java
A method which is declared as abstract and does not have
implementation is known as an abstract method.

Example of abstract method

abstract void printStatus();//no method body and abstract

Example of Abstract class that has an abstract


method
In this example, Bike is an abstract class that contains only one abstract
method run. Its implementation is provided by the Honda class.

abstract class Bike


{
abstract void run();
}
class Honda4 extends Bike
{
void run()
{
System.out.println("running safely");
}
public static void main(String args[])
{
Bike obj = new Honda4();
obj.run();
}
}
Output:

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.

Example 1: Java Encapsulation

class Area {

// fields to calculate area

int length;

int breadth;

// constructor to initialize values

Area(int length, int breadth) {

this.length = length;

this.breadth = breadth;

// method to calculate area

public void getArea() {

int area = length * breadth;

System.out.println("Area: " + area);

class Main {

public static void main(String[] args) {

// create object of Area

// pass value of length and breadth


Area rectangle = new Area(5, 6);

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.

It helps to control the values of our data fields. For example,

class Person {

private int age;

public void setAge(int age) {

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,

getName() // provides read-only access


setName() // provides write-only access

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,

Example 2: Data hiding using the private specifier

class Person {

// private field

private int age;

// getter method

public int getAge() {

return age;

// setter method

public void setAge(int age) {

this.age = age;

class Main {

public static void main(String[] args) {

// create an object of Person

Person p1 = new Person();

// change age using setter


p1.setAge(24);

// access age using getter

System.out.println("My age is " + p1.getAge());

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.

// error: age has private access in Person

p1.age = 24;

Inheritance in Java

Inheritance is one of the key features of OOP that allows us to create a


new class from an existing class.

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
}

// use of extends keyword


// to perform inheritance
class Dog extends Animal {

// methods and fields of Animal


// methods and fields of Dog
}

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 {

// field and method of the parent class


String name;
public void eat() {
System.out.println("I can eat");
}
}

// inherit from Animal


class Dog extends Animal {

// new method in subclass


public void display() {
System.out.println("My name is " + name);
}
}

class Main {
public static void main(String[] args) {

// create an object of the subclass


Dog labrador = new Dog();

// access field of superclass


labrador.name = "Rohu";
labrador.display();

// call method of superclass


// using object of subclass
labrador.eat();

}
}

Output

My name is Rohu
I can eat

In the above example, we have derived a subclass Dog from


superclass Animal .

In the above example, we have derived a subclass Dog from


superclass Animal .

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 {

// method to render a shape


public void render() {
System.out.println("Rendering Polygon...");
}
}

class Square extends Polygon {

// renders Square
public void render() {
System.out.println("Rendering Square...");
}
}

class Circle extends Polygon {

// renders circle
public void render() {
System.out.println("Rendering Circle...");
}
}

class Main {
public static void main(String[] args) {

// create an object of Square


Square s1 = new Square();
s1.render();

// create an object of Circle


Circle c1 = new Circle();
c1.render();
}
}
Run Code

Output
Rendering Square...
Rendering Circle...

In the above example, we have created a superclass: Polygon and two


subclasses: Square and Circle. Notice the use of the render() method.
The main purpose of the render() method is to render the shape. However, the process of
rendering a square is different than the process of rendering a circle.Hence,
the render() method behaves differently in different classes. Or, we can say render() is
polymorphic.

You might also like