Php Unit 1 (1)
Php Unit 1 (1)
Object
3. Data Abstraction:
Data abstraction is one of the most essential and important
features of object-oriented programming. Data abstraction
refers to providing only essential information about the data to
the outside world, hiding the background details or
implementation. Consider a real-life example of a man driving a
car. The man only knows that pressing the accelerators will
increase the speed of the car or applying brakes will stop the
car, but he does not know about how on pressing the
accelerator the speed is increasing, he does not know about the
inner mechanism of the car or the implementation of the
accelerator, brakes, etc in the car. This is what abstraction is.
4. Encapsulation:
Encapsulation is defined as the wrapping up of data under a
single unit. It is the mechanism that binds together code and
the data it manipulates. In Encapsulation, the variables or data
of a class are hidden from any other class and can be accessed
only through any member function of their class in which they
are declared. As in encapsulation, the data in a class is hidden
from other classes, so it is also known as data-hiding.
6. Polymorphism:
The word polymorphism means having many forms. In simple
words, we can define polymorphism as the ability of a message
to be displayed in more than one form. For example, A person
at the same time can have different characteristics. Like a man
at the same time is a father, a husband, an employee. So the
same person posses different behavior in different situations.
This is called polymorphism.
7. Dynamic Binding:
In dynamic binding, the code to be executed in response to the
function call is decided at runtime. Dynamic binding means that
the code associated with a given procedure call is not known
until the time of the call at run time. Dynamic Method Binding
One of the main advantages of inheritance is that some derived
class D has all the members of its base class B. Once D is not
hiding any of the public members of B, then an object of D can
represent B in any context where a B could be used. This
feature is known as subtype polymorphism.
8. Message Passing:
It is a form of communication used in object-oriented
programming as well as parallel programming. Objects
communicate with one another by sending and receiving
information to each other. A message for an object is a request
for execution of a procedure and therefore will invoke a function
in the receiving object that generates the desired results.
Message passing involves specifying the name of the object,
the name of the function, and the information to be sent.
Java Syntax
Java is an object-oriented programming language which is
known for its simplicity, portability, and robustness. The syntax
of Java programming language is very closely aligned with C
and C++ which makes it easier to understand.
Java Syntax refers to a set of rules that defines how Java
programs are written and interpreted by the compiler. These
rules ensure that your code is readable, logically correct, and
error-free as well. Now, Let’s understand the Syntax and
Structure of Java Programs with a basic “Hello World” program.
Structure of Java Program
A basic Java program consists of several components that
create a functional application. W:e can learn about basic Java
Syntax using the following program:
// FileName : "Test.java".
Output
Hello World
Explanation: The above program shows the basic Java
program that contains Class declaration, Main Method,
Statements, etc. Let’s try to understand them one by one.
Terminologies of a Basic Java Program
1. Class Declaration: Every Java program starts with a class
declaration using the class keyword. “A class is a blue print
of an object” and we can also define class as a logical
template that share common properties and methods.
2. Object: The object is an instance of a class. It is an entity
that has behavior and state.
Example: Dog, Cat, Monkey etc. are the object of “Animal”
class.
Behavior: Running on the road.
3. Main Method: The public static void main(String
args[]) method is the entry point where the program starts
execution.
4. Statements: Each line of code inside the method must end
with a semicolon(;)
Steps to Compile and Run a Java Program in a Console
1. Run the javac command to compile the Java Program
javac Test.java
The java compiler(javac) reads the source code(Test.java) and
generates a bytecode file(Test.class) in the same directory
2. Use the java command to execute the complied
bytecode
java Test
Note:
Do include the .class extension in the command.
Make sure Test.class file is in the current directory.
Java Syntax Key Elements
1. Comments in Java
There are three types of comments in Java.
Single line Comment
// This is a single-line comment
Multi-line Comment
/* This is
a multi-line comment */
Documentation Comment
It is also known as doc comment
/** This is a doc comment */
2. Source File Name
Source File Name Rule in Java
1. When There is a public class in the file
The name of a source file must exactly match the name of the
public class name with the extension of . java
Example: Assume you have a public class name GFG.
import java.io.*;
class GFG
{
public static void main (String[] args)
{
// declaring two int variables
int a = 10;
int b = 20;
System.out.println( a + b );
}
}
Output
30
Now, let us explore different types of primitive and non-
primitive data types.
twos-
8
compleme 0 (none) -128 to 127
bits
byte nt integer
‘a’, ‘\ characters
u0041’, representation
Unicode \ 16 of ASCII
‘\101’,
character u0000 bits values
‘\\’, ‘\’,
char ‘\n’, ‘β’ 0 to 255
twos-
16 -32,768 to
compleme 0 (none)
bits 32,767
short nt integer
-
twos- 2,147,483,648
32 -2,-
compleme 0
bits 1,0,1,2 to
nt intger
int 2,147,483,647
,854,775,807
1.23e1
00f , -
IEEE 754
32 1.23e- upto 7
floating 0.0
bits 100f , . decimal digits
point
3f ,3.14
float F
1.2345
6e300d
IEEE 754 ,-
64 upto 16
floating 0.0 123456
bits decimal digits
point e-
300d ,
double 1e1d
class GFG
{
public static void main(String args[])
{
Output
char: G
integer: 89
byte: 4
short: 56
float: 4.7333436
double: 4.355453532
long: 12121
Non-Primitive (Reference) Data Types
The Non-Primitive (Reference) Data Types will contain a
memory address of variable values because the reference
types won’t store the variable value directly in memory. They
are strings, objects, arrays, etc.
1. Strings
Strings are defined as an array of characters. The difference
between a character array and a string in Java is, that the string
is designed to hold a sequence of characters in a single
variable whereas, a character array is a collection of separate
char-type entities. Unlike C/C++, Java strings are not
terminated with a null character.
Syntax: Declaring a string
<String_Type> <string_variable> = “<sequence_of_string>”;
Example:
// Declare String without using new operator
String s = "GeeksforGeeks";
// Declare String using new operator
String s1 = new String("GeeksforGeeks");
2. Class
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. In general,
class declarations can include these components, in order:
1. Modifiers : A class can be public or has default access.
Refer to access specifiers for classes or interfaces in Java
2. Class name: The name should begin with an initial letter
(capitalized by convention).
3. Superclass(if any): The name of the class’s parent
(superclass), if any, preceded by the keyword extends. A
class can only extend (subclass) one parent.
4. Interfaces(if any): A comma-separated list of interfaces
implemented by the class, if any, preceded by the
keyword implements. A class can implement more than
one interface.
5. Body: The class body is surrounded by braces, { }.
3. Object
An Object is a basic unit of Object-Oriented Programming and
represents real-life entities. A typical Java program creates
many objects, which as you know, interact by invoking
methods. An object consists of :
1. State : It is represented by the attributes of an object. It
also reflects the properties of an object.
2. Behavior : It is represented by the methods of an object.
It also reflects the response of an object to other objects.
3. Identity : It gives a unique name to an object and enables
one object to interact with other objects.
4. Interface
Like a class, an interface can have methods and variables, but
the methods declared in an interface are by default abstract
(only method signature, no body).
Interfaces specify what a class must do and not how. It is
the blueprint of the class.
An Interface is about capabilities like a Player may be an
interface and any class implementing Player must be able
to (or must implement) move(). So it specifies a set of
methods that the class has to implement.
If a class implements an interface and does not provide
method bodies for all functions specified in the interface,
then the class must be declared abstract.
A Java library example is Comparator Interface . If a class
implements this interface, then it can be used to sort a
collection.
5. Array
An Array is a group of like-typed variables that are referred to
by a common name. Arrays in Java work differently than they
do in C/C++. The following are some important points about
Java arrays.
In Java, all arrays are dynamically allocated. (discussed
below)
Since arrays are objects in Java, we can find their length
using member length. This is different from C/C++ where
we find length using size.
A Java array variable can also be declared like other
variables with [] after the data type.
The variables in the array are ordered and each has an
index beginning with 0.
Java array can also be used as a static field, a local
variable, or a method parameter.
The size of an array must be specified by an int value and
not long or short.
The direct superclass of an array type is Object.
Every array type implements the
interfaces Cloneable and java.io.Serializable.
Java Variables
Variables are the containers for storing the data values or you
can also call it a memory location name for the data. Every
variable has a:
Data Type – The kind of data that it can hold. For
example, int, string, float, char, etc.
Variable Name – To identify the variable uniquely within
the scope.
Value – The data assigned to the variable.
There are three types of variables in Java – Local,
Instance, and Static.
Example:
int age = 27; // integer variable having value 27
Example:
// Declaring float variable
float simpleInterest;
class GFG {
public static void main(String[] args)
{
// Declared a Local Variable
int var = 10;
Output
Local Variable: 10
Example 2:
// Java Program to show the use of
// Local Variables
import java.io.*;
public class GFG {
public static void main(String[] args)
{
// x is a local variable
int x = 10;
if (x > 5) {
// result is a
// local variable
String result = "x is greater than 5";
System.out.println(result);
}
Output
x = 10
message = Hello, world!
x is greater than 5
Iteration 0
Iteration 1
Iteration 2
2. Instance Variables
Instance variables are non-static variables and are declared in a
class outside of any method, constructor, or block.
As instance variables are declared in a class, these
variables are created when an object of the class is
created and destroyed when the object is destroyed.
Unlike local variables, we may use access specifiers for
instance variables. If we do not specify any access
specifier, then the default access specifier will be used.
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.
Scope of instance variables are throughout the class
except the static contexts.
Instance variables can be accessed only by creating
objects.
We initialize instance variables using constructors while
creating an object. We can also use instance blocks to
initialize the instance variables.
Example:
// Java Program to show the use of
// Instance Variables
import java.io.*;
class GFG {
// Main Method
public static void main(String[] args)
{
// Object Creation
GFG name = new GFG();
// Displaying O/P
System.out.println("Geek name is: " + name.geek);
System.out.println("Default value for int is "+ name.i);
class GFG {
// Declared static variable
public static String geek = "Shubham Jain";
// static int c = 0;
// above line, when uncommented,
// will throw an error as static variables cannot be
// declared locally.
}
}
Output
Geek Name is : Shubham Jain
Instance Variables vs Static Variables
Now let us discuss the differences between the Instance
variables and the Static variables:
Each object will have its own copy of an instance variable,
whereas we can only have one copy of a static variable
per class, irrespective of how many objects we create.
Thus, static variables are good
for memory management.
Changes made in an instance variable using one object
will not be reflected in other objects as each object has its
own copy of the instance variable. In the case of a static
variable, changes will be reflected in other objects as
static variables are common to all objects of a class.
We can access instance variables through object
references, and static variables can be accessed directly
using the class name.
Instance variables are created when an object is created
with the use of the keyword ‘new’ and destroyed when the
object is destroyed. Static variables are created when the
program starts and destroyed when the program stops.
Syntax: Static and instance variables
class GFG
{
// Static variable
static int a;
// Instance variable
int b;
}