KEMBAR78
Notes 1 | PDF | Object Oriented Programming | Variable (Computer Science)
0% found this document useful (0 votes)
19 views14 pages

Notes 1

Uploaded by

jattubhai0509
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views14 pages

Notes 1

Uploaded by

jattubhai0509
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

OOP Concepts

Object Oriented Programming is a paradigm that provides many concepts such as


inheritance, data binding, polymorphism etc.

Simula is considered as the first object-oriented programming language. The programming paradigm
where everything is represented as an object is known as truly object-oriented programming language.

Smalltalk is considered as the first truly object-oriented programming language.

OOPs (Object Oriented Programming System)

Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is
a methodology or paradigm to design a program using classes and objects. It simplifies the software
development and maintenance by providing some concepts:

o Object
o Class
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation

Object

Any entity that has state and behavior is known as an object. For example: chair, pen, table,
keyboard, bike etc. It can be physical and logical.

Class

Collection of objects is called class. It is a logical entity.

Inheritance

When one object acquires all the properties and behaviours of parent object i.e. known as
inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

JAVA PROGRAMMING Page 1


Polymorphism

When one task is performed by different ways i.e. known as polymorphism. For example: to
convince the customer differently, to draw something e.g. shape or rectangle etc.

In java, we use method overloading and method overriding to achieve polymorphism.

Another example can be to speak something e.g. cat speaks meaw, dog barks woof etc.

Abstraction

Hiding internal details and showing functionality is known as abstraction. For example: phone
call, we don't know the internal processing.

In java, we use abstract class and interface to achieve abstraction.

Encapsulation

Binding (or wrapping) code and data together into a single unit is known as encapsulation.
For example: capsule, it is wrapped with different medicines.

A java class is the example of encapsulation. Java bean is the fully encapsulated class because all
the data members are private here.

Benefits of Inheritance

 One of the key benefits of inheritance is to minimize the amount of duplicate code in an
application by sharing common code amongst several subclasses. Where equivalent code

exists in two related classes, the hierarchy can usually be refactored to move the common

code up to a mutual superclass. This also tends to result in a better organization of code and
smaller, simpler compilation units.

 Inheritance can also make application code more flexible to change because classes that

inherit from a common superclass can be used interchangeably. If the return type of a
method is superclass
 Reusability - facility to use public methods of base class without rewriting the same.
 Extensibility - extending the base class logic as per business logic of the derived class.

JAVA PROGRAMMING Page 2


 Data hiding - base class can decide to keep some data private so that it cannot be

altered by the derived class


Procedural and object oriented programming paradigms

JAVA PROGRAMMING Page 3


Java Programming- History of Java

The history of java starts from Green Team. Java team members (also known as
Green Team), initiated a revolutionary task to develop a language for digital devices
such as set-top boxes, televisions etc.

For the green team members, it was an advance concept at that time. But, it was
suited for internet programming. Later, Java technology as incorporated by
Netscape.

Currently, Java is used in internet programming, mobile devices, games, e-business


solutions etc. There are given the major points that describes the history of java.

1) James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java
language project in June 1991. The small team of sun engineers called Green Team.

2) Originally designed for small, embedded systems in electronic appliances like set-
top boxes.

3) Firstly, it was called "Greentalk" by James Gosling and file extension was .gt.

4) After that, it was called Oak and was developed as a part of the Green
project.

Java Version History


From the first version released in 1996 to the latest version 18 released in 2022, the Java
platform has been actively being developed for about nearly 27 years. Many changes and
improvements have been made to the technology over the years. The following table
summarizes all versions of Java SE from its early days to the latest .

Java SE Version Version Number Release Date

JDK 1.0(Oak) 1.0 January 1996

JDK 1.1 1.1 February 1997

J2SE 1.2(Playground) 1.2 December 1998

J2SE 1.3(Kestrel) 1.3 May 2000

J2SE 1.4(Merlin) 1.4 February 2002

J2SE 5.0(Tiger) 1.5 September 2004


Java SE 6(Mustang) 1.6 December 2006

JAVA PROGRAMMING Page 4


Java SE 7(Dolphin) 1.7 July 2011

Java SE 8 1.8 March 2014

Java SE 9 9 September, 21st 2017

Java SE 10 10 March, 20th 2018

Java SE 11 11 September, 25th 2018

Java SE 12 12 March, 19th 2019

Java SE 13
13 September, 17th 2019

Java SE 14 14 March, 17th 2020

Java SE 15 15 September, 15th 2020

Java SE 16 16 March, 16th 2021

Java SE 17 17 September, 14th 2021

Java SE 18 March, 22nd 2022


18

From the table above we can see that the naming and the version number have been
changing over times:

JAVA PROGRAMMING Page 5


Features of Java
There is given many features of java. They are also known as java buzzwords. The Java Features
given below are simple and easy to understand.
1. Simple
2. Object-Oriented
3. Portable
4. Platform independent
5. Secured
6. Robust
7. Architecture neutral
8. Dynamic
9. Interpreted
10. High Performance
11. Multithreaded
12. Distributed

Java Comments

The java comments are statements that are not executed by the compiler and interpreter. The
comments can be used to provide information or explanation about the variable, method, class or
any statement. It can also be used to hide program code for specific time.

Types of Java Comments

There are 3 types of comments in java.

1. Single Line Comment


2. Multi Line Comment
3. Documentation Comment

Java Single Line Comment

The single line comment is used to comment only one line.

Syntax:

1. //This is single line comment

JAVA PROGRAMMING Page 6


Example:

public class CommentExample1 {


public static void main(String[] args) {
int i=10;//Here, i is a variable
System.out.println(i);
}
}

Output:

10

Java Multi Line Comment

The multi line comment is used to comment multiple lines of code.

Syntax:

/*
This
is
multi line
comment
*/

Example:

public class CommentExample2 {


public static void main(String[] args) {
/* Let's declare and
print variable in java. */
int i=10;
System.out.println(i);
}}

Output:

10

JAVA PROGRAMMING Page 7


Java Documentation Comment

The documentation comment is used to create documentation API. To create documentation API, you need
to use javadoc tool.

Syntax:

/**
This
is
documentation
comment
*/

Example:

/** The Calculator class provides methods to get addition and subtraction of given 2 numbers.*/
public class Calculator {
/** The add() method returns addition of given numbers.*/
public static int add(int a, int b){return a+b;}
/** The sub() method returns subtraction of given numbers.*/
public static int sub(int a, int b){return a-b;}
}

Compile it by javac tool:

javac Calculator.java

Create Documentation API by javadoc tool:

javadoc Calculator.java

Now, there will be HTML files created for your Calculator class in the current directory. Open the HTML
files and see the explanation of Calculator class provided through documentation comment.

JAVA PROGRAMMING Page 8


Data Types
Data types represent the different values to be stored in the variable. In java, there are two types of data types:

o Primitive data types


o Non-primitive data types

Data Type Default Value Default size

boolean False 1 bit

char '\u0000' 2 byte

byte 0 1 byte

short 0 2 byte

int 0 4 byte

long 0L 8 byte

float 0.0f 4 byte

double 0.0d 8 byte

Java Variable Example: Add Two Numbers


class Simple{
public static void main(String[] args)
{
int a=10;
int b=10;
int c=a+b;
System.out.println(c);
}}

Output:20

JAVA PROGRAMMING Page 9


Variables and Data Types in Java
Variable is a name of memory location. There are three types of variables in java: local, instance
and static.

There are two types of data types in java: primitive and non-primitive.

Types of Variable
There are three types of variables in java:

o local variable
o instance variable
o static variable

1) Local Variable

A variable which is declared inside the method is called local variable.

2) Instance Variable

A variable which is declared inside the class but outside the method, is called instance variable . It
is not declared as static.

3) Static variable

A variable that is declared as static is called static variable. It cannot be local.

We will have detailed learning of these variables in next chapters.

Example to understand the types of variables in java

class A
{
int data=50;//instance variable
static int m=100;//static variable
void method(){
int n=90;//local variable
}
}//end of class

Constants in Java

A constant is a variable which cannot have its value changed after declaration. It uses the 'final'
keyword.

Syntax
modifier final dataType variableName = value; //global constant

modifier static final dataType variableName = value; //constant within a c

JAVA PROGRAMMING Page 10


Scope and Life Time of Variables
The scope of a variable defines the section of the code in which the variable is visible. As a general
rule, variables that are defined within a block are not accessible outside that block. The lifetime
of a variable refers to how long the variable exists before it is destroyed. Destroying variables
refers to deallocating the memory that was allotted to the variables when declaring it. We have
written a few classes till now. You might have observed that not all variables are the same. The
ones declared in the body of a method were different from those that were declared in the class
itself. There are three types of variables: instance variables, formal parameters or local variables
and local variables.

Instance variables

Instance variables are those that are defined within a class itself and not in any method or
constructor of the class. They are known as instance variables because every instance of the class
(object) contains a copy of these variables. The scope of instance variables is determined by the
access specifier that is applied to these variables. We have already seen about it earlier. The
lifetime of these variables is the same as the lifetime of the object to which it belongs. Object
once created do not exist for ever. They are destroyed by the garbage collector of Java when there
are no more reference to that object. We shall see about Java's automatic garbage collector later
on.

Argument variables

These are the variables that are defined in the header oaf constructor or a method. The scope
of these variables is the method or constructor in which they are defined. The lifetime is limited
to the time for which the method keeps executing. Once the method finishes execution, these
variables are destroyed.

Local variables

A local variable is the one that is declared within a method or a constructor (not in the header).
The scope and lifetime are limited to the method itself.

One important distinction between these three types of variables is that access specifiers can
be applied to instance variables only and not to argument or local variables.

In addition to the local variables defined in a method, we also have variables that are defined in
bocks life an if block and an else block. The scope and is the same as that of the blockitself.

JAVA PROGRAMMING Page 11


Operators in java

Operator in java is a symbol that is used to perform operations. For example: +, -, *, / etc.

There are many types of operators in java which are given below:

o Unary Operator,
o Arithmetic Operator,
o shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator.

Operators Hierarchy

JAVA PROGRAMMING Page 12


Expressions
Expressions are essential building blocks of any Java program, usually created to produce a new
value, although sometimes an expression simply assigns a value to a variable. Expressions are
built using values, variables, operators and method calls.

Types of Expressions

While an expression frequently produces a result, it doesn't always. There are three types of
expressions in Java:

 Those that produce a value, i.e. the result of (1 + 1)


 Those that assign a variable, for example (v = 10)
 Those that have no result but might have a "side effect" because an expression can include
a wide range of elements such as method invocations or increment operators that modify
the state (i.e. memory) of a program.

Java Type casting and Type conversion

Widening or Automatic Type Conversion


Widening conversion takes place when two data types are automatically converted. This happens
when:
 The two data types are compatible.
 When we assign value of a smaller data type to a bigger data type.

For Example, in java the numeric data types are compatible with each other but no automatic
conversion is supported from numeric type to char or boolean. Also, char and boolean are not
compatible with each other.

Narrowing or Explicit Conversion


If we want to assign a value of larger data type to a smaller data type we perform explicit type
casting or narrowing.
 This is useful for incompatible data types where automatic conversion cannot be done.
 Here, target-type specifies the desired type to convert the specified value to.



JAVA PROGRAMMING Page 13






Java Enum

Enum in java is a data type that contains fixed set of constants.

It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY and SATURDAY) , directions (NORTH, SOUTH, EAST and WEST)
etc. The java enum constants are static and final implicitly. It is available from JDK 1.5.

Java Enums can be thought of as classes that have fixed set of constants.

Simple example of java enum


class EnumExample1{
public enum Season { WINTER, SPRING, SUMMER, FALL }

public static void main(String[] args) {


for (Season s : Season.values())
System.out.println(s);
}}
Output:
WINTER
SPRING
SUMMER
FALL

JAVA PROGRAMMING Page 14

You might also like