MOD1 PPT-chap 1
MOD1 PPT-chap 1
MOD1 PPT-chap 1
AN OVERVIEW OF JAVA
OOP(Object Oriented Programming)
• Object oriented programming (OOP) is the core of
Java programming.
• While object-oriented programming is about
creating objects that contain both data and
methods.
• Java is a general purpose, object- oriented
programming language developed by Sun
Microsystems.
• It was invented by James Gosling and his team and
was initially called as Oak.
Two Paradigms:
• Every program contains 2 components code and data.
• Two approaches are there to solve the problem and in
program writing: Procedure oriented and object
oriented.
Procedure Oriented:
• Procedure oriented programs are written based on
―whats happening‖ around, where the code acts on
data. Ex: C etc
Object Oriented:
• Object oriented programs are written based on ―Who
is being affected‖ around, which manages the
increasing complexity.
• Ex: C++, JAVA, Small Talk etc
• C
Class a{
Printf(“Welcome”); // Procedure Oriented:
}
• C++
Class a{
Cout<<welcome; //Object Oriented:
}
The Three OOP:
The three important features of OOP are:
• Encapsulation
• Inheritence
• Polymorphism
Encapsulation
• It is a process of wrapping up the data
members along with related data handlers
method is called Encapsulation.
• Wrapping up the data members and data
handlers method means we are grouping both
as a single unit. In Java, we group or wrap
both inside the Class, so Class is a single unit
that consists of data members as well as
methods.
Ex
Ex
Note:
This keyword
• this can be used to refer current class instance variable.
• this can be used to invoke current class method
(implicitly)
• this() can be used to invoke current class constructor.
• this can be passed as an argument in the method call.
• this can be passed as argument in the constructor call.
• this can be used to return the current class instance
from the method.
Inheritence:
• Inheritence is the process by which one object
acquires the properties of another object. This
is important as it supports the concept of
hierarchical classification.
• By the use of inheritence, sub class derived
from the parent class or base class.
• Ex: A child inheriting properties from parents
• Single Inheritance:
• Multiple Inheritance:
• Multilevel Inheritance
• Hierarchical Inheritance:
• Hybrid Inheritance:
Single Inheritance:
Multilevel Inheritance
Deriving the class from another derived class:
class C extend class B and class C extend class A
Parent class is inherited by many sub classes
Multiple Inheritance
Deriving the class from more than one direct base class
Hybrid Inheritance
More than one kind of the inheritance is observed
Polymorphism
• polymorphism refers it is having same name and
multiple forms
• Polymorphism (meaning many forms)
• Polymorphism uses those methods to perform
different tasks. This allows us to perform a single
action in different ways.
• It performs based on arguments
• Ex: “+”‖ can be used for addition of 2 numbers and
also concatenation of 2 strings.
System.out.println(2+4); // outputs 6 as answer
System.out.println(“Hello‖” +”vandana‖”); // outputs
Hello vanadana as answer
• 2 or more methods in a same class have the
same name but different parameter//
overloading
Method overloading
method add(float a,float b)
{
Return a+b
}
method add(int a,int b,int c)
{
Return a+b+c
}
method add(string a,string b)
{
Return a+b
• Name and parameters are same in the
superclass and child class // overriding
Operator overloading
Operator + (int a,int b)
{
Return a+b
}
Operator + (string a,string b)
{
Return a+b
}
• Child class has the same method as declared
in the parent class.// method overriding
override
Class A{
Method add(int a,int b)
{
Return a+b
}
Class B extend class A{
Override Method add( int x,int y){
Return x+y
}
}
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
}
}
Output:
The animal makes a sound
The pig says: wee wee
The dog says: bow wow
Object:
Abstract method:
can only be used in an abstract class, and it does not have a
body. The body is provided by the subclass (inherited from).
// Abstract class
abstract class Animal {
// Abstract method (does not have a body)
public abstract void animalSound();
// Regular method
public void sleep() {
System.out.println("Zzz");
}
}
class Example2//declaration
{
public static void main(String args[]) //define main method
{
int n=3; //initialized
System.out.println(“the value of n is “+n);//input the line
n=n+5; //assigned the value(appending or add)
System.out.print(“ the new value is”); //display ,newline
System.out.println(n); //display
}
}
Output: the value of n is 3//o/p
Two Control Statements
• Here in this chapter initially we focus on two
control statements if and for loop , the detailed
control statements will be discussed in module 2.
if statement
• The if- statement is the most basic of all the
control flow statements.
• It tells your program to execute a certain section
of code only if a particular test evaluates to true.
• Here is the general form of the if statement:
if (condition) :
statement;
• Here the condition is Boolean expression.
• If the condition is true then the statement is
executed, if false then statement will be
skipped.
Example:
class Example
{
public static void main(String args[])
{
int a=5;
if(a>0)
System.out.println(“a is positive number‖”);
System.out.println(“End of program”‖);
}
}
class If Sample {
public static void main(String args[]){
int x, y;
x = 10;
y = 20;
if(x < y)
System.out.println("x is less than y");
x = x * 2;
if(x == y) System.out.println("x now equal to y");
x = x * 2;
if(x > y)
System.out.println("x now greater than y"); // this won't display anything
if(x == y)
System.out.println("you won't see this");
}}
The output generated by this program is shown here:
x is less than y
x now equal to y
x now greater than y
The for loop
• The for loop is similar to that of C/C++
• Here is the general form of the traditional for
statement:
for(initialization; condition; iteration)
{
//body
}
• Initialization sets the loop control variable to
initial value.
• Condition is a Boolean expression which tests
the loop
• Iteration expression tells how the control
variable has to change at each iteration.
Generally the increment or decrement
operator is used to perform iteration.
Example:
class Example
{
public static void main(String args[])
{
int a;
for(a=0;a<5;a++)
System.out.println(a);
System.out.println(“ End of program”);
}
}
o/p:
Output: 0
1
2
3
4
Using blocks of code
• Java supports code blocks - which means that two
or more statements are grouped into blocks of
code.
• Section of the code enclosed in curly brackets{}.
• Opening and closing braces is used to achieve this.
• Each block is treated as logical unit.
• Whenever two or more statements has to be
linked blocks can be used.
class Example1
{
public static void main(String args[])
{
int x=10;
Int y=20;
System.out.println(“The answer is”);
System.out.println(x+y);
System.out.println(“End program”);
//A code block enclosing a multiple statement
Lexical issues:
• A lexical token may consist of one or more
characters, and every single character is in
exactly one token.
• The tokens can be keywords, comments,
numbers, white space, or strings. All lines
should be terminated by a semi-colon (;).
• Java programs are a collection of whitespace,
identifiers, literals, comments, operators,
separators, and keywords.
Whitespace:
• Java is a free from language- means no need
to follow any indentation rules.
• It returns T or F/ Boolean value
• Whitespace is a space, tab, or newline
• \b
• \n
• \t
Java character set:
• The smallest unit of Java language are its
character set used to write Java tokens.
Key Words:
Syntax:
type variableName = value;
Example 2:
String name = "John";
System.out.println(name);
Initializing a variable:
• followed by the variable name, followed by an
equal sign, followed by an expression.
• variable can be initialize in two ways.
They are
• (a) Initializing by Assignment statements.
• (b) Dynamic Initialisation
Initializing by assignment statements:
One variable can be initialize using assignment
statements.
• datatype: Type of data that can be stored in this
variable.
• variable_name: Name given to the variable.
• value: It is the initial value stored in the variable.
The syntax is : Variable-name = Value;
Example:
• int a=10,b,c=16;
• Double pi=3.147;
// Declaring float variable
float simpleInterest;
Example:
class Example {
public static void main(String args[])
{
double a=10, b=2.6;
double c=a/b;
System.out.println(“value of c is”+c);
}
}
Types of Variables in Java
Different types of variables
• Local Variables
• Instance Variables
• Static Variables
Local Variables
• A variable that is only accessible within a specific part of a
program.
• A variable defined within a block or method or constructor is
called a local variable.
• These variables are created when the block is entered, or the
function is called and destroyed after exiting from the block or
when the call returns from the function.
• The scope of these variables exists only within the block in
which the variables are declared, i.e., we can access these
variables only within that block.
• Initialization of the local variable is mandatory before using it in
the defined scope.
Example
// Java Program to implement
// Local Variables
import java.io.*;
class Local{
public static void main(String[] args)
{
// Declared a Local Variable
int var = 10
// This variable is local to this main method only
System.out.println("Local Variable: " + var);
}
}
Output
Local Variable: 10
2.Instance Variables
• Instance variables are non-static variables and are
declared in a class but outside of any method,
constructor, or block.
• This variable is created when an object is instantiated
and accessible to all constructor, methods or blocks in
the class.
• Initialization of an instance variable is not mandatory.
Its default value is dependent on the data type of
variable. For String it
is null, for float it is 0.0f, for int it is 0, for Wrapper
classes like Integer it is null, etc.
• Instance variables can be accessed only by creating
Example
// Java Program to demonstrate // Main Method
// Instance Variables public static void main(String[] args)
{
import java.io.*;
// Object Creation
class Instance{ Instance name = new Instance ();
// Declared Instance Variable // Displaying O/P
public String Pname; System.out.println(“Person name is: " +
public int i; name.Pname);
System.out.println("Default value for int
public Integer I; is: "+ name.i);
public Instance()
{ // toString() called internally
// initializing Instance Variable System.out.println("Default value for
Integer is: "+ name.I);
this.Pname = “Adya Jain";
}
} }
Output:
Person name is: Adya Jain
Default value for int is: 0
Default value for Integer is: null
3. Static Variables
• Static variables are also known as class variables.
• It is declared as static. Single copy of the copy is
created.
• A variable that has been allocated "statically",
meaning that its lifetime (or "extent") is the
entire run of the program.
• The difference is that static variables are
declared using the static keyword within a class
outside of any method, constructor, or block.
Exam p le
Example:
Creating/Initializing Arrays: