0% found this document useful (0 votes)
2 views

Unit-2 an Overview of Java

This document provides an overview of Java, focusing on Object-Oriented Programming (OOP) principles such as encapsulation, inheritance, and polymorphism. It also discusses lexical issues in Java, including identifiers, literals, comments, and reserved words. The content aims to explain the structure and functionality of Java programming through its core concepts and programming paradigms.

Uploaded by

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

Unit-2 an Overview of Java

This document provides an overview of Java, focusing on Object-Oriented Programming (OOP) principles such as encapsulation, inheritance, and polymorphism. It also discusses lexical issues in Java, including identifiers, literals, comments, and reserved words. The content aims to explain the structure and functionality of Java programming through its core concepts and programming paradigms.

Uploaded by

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

An overview of

Java
Unit-2
Topics Covered
● Object-Oriented Programming
○ Two Paradigms
○ Abstraction
○ The Three OOP Principles
● Lexical Issues
○ Whitespace
○ Identifiers
○ Literals
○ Comments
○ Separators
○ The Java Keywords
Object Oriented Programming
Object Oriented programming (OOP) is a programming paradigm that relies
on the concept of classes and objects.

It is used to structure a software program into simple, reusable pieces of code


blueprints (usually called classes), which are used to create individual
instances of objects.

All the program written in java are based on Object-Oriented Concept


Major Programming Paradigms
There are several kinds of major programming paradigms:

● Imperative
● Logical
● Functional
● Object-Oriented
Two Paradigms
All programs consist of two elements, namely code and data

Programs can be written being focused on code or data

When programs are written focusing on code, it is referred as process-


oriented model. While the programs written focusing on data, it is object-
oriented programming

Process- oriented model attempts to answer questions regarding “what is


happening”. Whereas, object-oriented model answers the question “who is
being affected.”
Process-oriented approach characterizes a program as a series of linear steps
(i.e., code). It can be thought of as code acting on data.

Object-oriented programming organizes a program around its data (that is,


objects) and a set of well-defined interfaces to that data. It can be
characterized as data controlling access to code

Object oriented concept helps to manage the complexities in process oriented


approach
Abstraction
Abstract means not complete or partially implemented.

Act of representing essential-features without including the details or


explanations

Abstraction is the process of hiding internal implementation or hide how


anything is implemented.

It emphasis on just highlighting the set of services we are offering.


The Three OOP Principles
● Encapsulation
● Inheritance
● Polymorphism
Encapsulation
● Encapsulation is the mechanism that binds together code and the data it manipulates,
and keeps both safe from outside interference and misuse.
● One way to think about encapsulation is as a protective wrapper that prevents the code
and data from being arbitrarily accessed by other code defined outside the wrapper.
● Access to the code and data inside the wrapper is tightly controlled through a well-
defined interface.
● In Java, the basis of encapsulation is the class.
● A class defines the structure and behavior (data and code) that will be shared by a set of
objects.
● Each object of a given class contains the structure and behavior defined by the class, as if
it were stamped out by a mold in the shape of the class.
● For this reason, objects are sometimes referred to as instances of a class. Thus, a class is a
logical construct; an object has physical reality.
● So the term data hiding is possible due to the concept of encapsulation, since the data are
hidden from the outside world so that it is safe from accidental alteration.
Inheritance
● Inheritance is the process by which objects of one class acquire the
characteristics of object of another class.
● In OOP, the concept of inheritance provides the idea of reusability.
● We can use additional features to an existing class without modifying it.
This is possible by deriving a new class (derived class) from the existing
one (base class).
● This process of deriving a new class from the existing base class is called
inheritance .
● It allows the extension and reuse of existing code without having to
rewrite the code.
Polymorphism
● Polymorphism means “having many forms”. The polymorphism allows
different objects to respond to the same message in different ways, the
response specific to the type of object.
● It allows one interface to be used for a general class of actions.
● The specific action is determined by the exact nature of the situation.
● Polymorphism is important when object oriented programs dynamically
creating and destroying the objects in runtime.
● More generally, the concept of polymorphism is often expressed by the
phrase “one interface, multiple methods.”
● Polymorphism in java is a concept by which we can perform a single
action by different ways.
● Polymorphism is derived from two Greek words: poly and morphs. The
word "poly" means many and "morphs" means forms.
● So polymorphism means having many forms.
● There are two types of polymorphism in java:
○ compile time polymorphism and runtime polymorphism.

● We can perform polymorphism in java by:


○ method overloading and method overriding.
Class and Object
● In Java everything is encapsulated under classes. Class is the core of Java
language. It can be defined as a template that describe the behaviors and states
of a particular entity.
● A class is a user defined blueprint or prototype from which objects are created.
● It represents the set of properties or methods that are common to all objects of
one type.
● We can think of the class as a sketch (prototype) of a house. It contains all the
details about the floors, doors, windows, etc. Based on these descriptions we
build the house.
● House is the object.
● Since many houses can be made from the same description, we can create many
objects from a class.
● Class can contain: methods, variables, constructor etc.
● An object is an instance of a class.

● Object is a real world entity that have properties like car, dog, pen table etc.

● An object has three characteristics:

● State: It is represented by attributes of an object. It also reflects the


properties of an object. It represent the data of the object.

● Behavior: represents the behavior (functionality) of an object such as


deposit, withdraw, etc. It is represented by methods of an object. It also
reflects the response of an object with other objects.

● Identity: It gives a unique name to an object and enables one object to


interact with other objects.
Example of an object: dog
Lexical Issues
Java programs are a collection of

● Whitespace
● Identifiers
● Literals
● Comments
● Operators
● Separators
● keywords
WhiteSpace
● Java is a free-form language
● For example, the program can be written all on one line or in any other
strange way you felt like typing it
● In Java, whitespace is a space, tab, or newline
Identifiers
A name in java program

It can be the method name, variable name, class name or label name.
Rules for defining java Identifier
● The only allowed characters in java identifiers are
○ a to z
○ A to Z
○ 0 to 9
○ $
○ _
● Note: If we use any other character, we will get compile time error.
Example:
total_number is valid.
total# is invalid.
Identifier can’t start with number/digits.
Example:
total123 is valid
123total is invalid
Identifier are case sensitive.

Example:

Class Test {

int number = 10;

int Number = 10;

int NUMBER = 10;

}
There is no limit to length for identifiers

We can’t use reserved words as identifiers.

Example:

int if = 10; invalid

int i = 10; valid

All predefined java class names and interface name can be used as identifiers.

Example:

int String = 0; valid

int Runnable = 10; valid


Which of the following are valid java identifiers?

1. total_number
2. total#
3. 123total
4. total123
5. ca$h
6. _$_$_$_$_$_$_
7. all@hands
8. java2share
9. Integer
10.Int
11.int
Literals
A constant value which can be assigned to a variable is called a literal
A constant value in Java is created by using a literal representation of it. For example,
here
are some literals:
100 98.6 ‘X’ “This is a test”
Left to right, the first literal specifies an integer, the next is a floating-point value, the
third
Is a character constant, and the last is a string. (integer literal, floating point literal,
character
literal and string literal )
A literal can be used anywhere a value of its type is allowed.
Comments
There are three types of comments defined by Java.

They are:

● Single-line Comment: //
● Multiline Comment : /*……..*/
● Documentation Comment /**………….*/

Documentation Comment is used to produce an HTML file that documents


the program

The documentation comment begins with a /** and ends with a */.
Separator
A separator is a symbol that is used to separate a group of code from one
another is called as separators in java.

In java, there are few characters that are used as a separator.

The most commonly used separator is a semicolon. As we have seen it is used


to terminate the statement.
Reserve Words
In any language some words are reserved to represents some kind of
meaning or functionality such words are reserved words.

In english there are numerous reserved words.

Likewise, in java some words are reserved to represent to represents some


meaning or functionality such type of words are called reserved words.

In Java, there is 53 reserved words


Self Study
Difference between process-oriented programming and object oriented
programming

Difference between class and objects

How Polymorphism, Encapsulation, and Inheritance Work Together?

Why the filename should be same as the class name in Java?

You might also like