Professional Documents
Culture Documents
Submitted By:
KOMAL AGARWAL
BACHELOR OF VOCATIONAL STUDIES(SOFTWARE DEVELOPMENT), 3
SEMESTER
03724818118
I have taken efforts in this project. However, it would not have been possible without the kind
support and help of many individuals and organizations. I would like to extend my sincere thanks to
all of them.
I am highly indebted to (Name of your Organization Guide) for their guidance and constant
supervision as well as for providing necessary information regarding the project & also for their
support in completing the project.
I would like to express my gratitude towards my parents & member of (Organization Name)for their
kind co-operation and encouragement which help me in completion of this project.
I would like to express my special gratitude and thanks to industry persons for giving me such
attention and time.
My thanks and appreciations also go to my colleague in developing the project and people who have
willingly helped me out with their abilities.
CONTENTS
What is Class?
A class is an entity that determines how an object will behave and what the object will
contain. In other words, it is a blueprint or a set of instruction to build a specific type
of object.
Syntax
EXAMPLE
void barking() {
}
void hungry() {
}
void sleeping() {
}
}
WHAT IS AN OBJECT?
Let us now look deep into what are objects. 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
behaviour.
If we consider a dog, then its state is - name, breed, color, and the behaviour is - barking,
wagging the tail, running.
If we compare the software object with a real-world object, they have very similar
characteristics.
Software objects also have a state and a behaviour. A software object's state is stored in
fields and behaviour 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.
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.
Following is an example of creating an object −
Example
Example
public class Puppy {
public Puppy(String name) {
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
Example
Following is an example that demonstrates how to achieve Encapsulation in Java −
/* File name : EncapTest.java */
public class EncapTest {
private String name;
private String idNum;
private int age;
The public setXXX() and getXXX() methods are the access points of the instance variables
of the EncapTest class. Normally, these methods are referred as getters and setters.
Therefore, any class that wants to access the variables should access them through these
getters and setters.
The variables of the EncapTest class can be accessed using the following program −
/* File name : RunEncap.java */
public class RunEncap {
public static void main(String args[]) {
EncapTest encap = new EncapTest();
encap.setName("James");
encap.setAge(20);
encap.setIdNum("12343ms");
Output
Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance,
polymorphism, and abstraction.
Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data
(methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other
classes, and can be accessed only through the methods of their current class. Therefore, it is also known
as data hiding.
Provide public setter and getter methods to modify and view the variables values.
Example
Normally, these methods are referred as getters and setters. Therefore, any class that wants to access
the variables should access them through these getters and setters.
The variables of the EncapTest class can be accessed using the following program −
Output
ADVANTAGES OF ENCAPSULATION
Getter and Setter Methods – Private member can only be accessed
within the same class. An outside class cannot access the data members of
that class. If you need to access these variables, you have to use public
“getter” and “setter” methods.
Flexibility – With this, we can make the data as read-only or write-only
as we require it to be. It also improves the maintainability and flexibility of
code.
Reusability – It allows the user to a programmer to use the existing code
again and again in an effective way.
Testing of the code – Ease of testing becomes easy.
Data Hiding – It can provide the programmer to hide the inner classes
and the user to give access only to the desired codes. It allows the
programmer to not allow the user to know how variables and data store.
INHERITANCE
Inheritance is a process of defining a new class based on an existing class by
extending its common data members and methods.
Inheritance allows us to reuse of code, it improves reusability in your java
application.
Single inheritance
Output
Multiple Inheritance: In Multilevel Inheritance, a derived class will be inheriting a base class
and as well as the derived class also act as the base class to other class. The class A serves as a base class
for the derived class B, which in turn serves as a base class for the derived class C. In Java, a class cannot
directly access the grandparent’s members.
import java.util.*;
import java.lang.*;
import java.io.*;
class one
{
public void print_geek()
{
System.out.println("Geeks");
}
}
// Drived class
public class Main
{
public static void main(String[] args)
{
three g = new three();
g.print_geek();
g.print_for();
g.print_geek();
}
}
OUTPUT
Geeks
for
Geeks
Polymorphism in Java
Polymorphism in Java is a concept by which we can perform a single action in different
ways. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly"
means many and "morphs" means forms. So polymorphism means 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.
TYPES OF POLYMORPHISM
Compile time polymorphism: It is also known as static polymorphism. This type of polymorphism is
achieved by function overloading or operator overloading.
Method Overloading: When there are multiple functions with same name but different
parameters then these functions are said to be overloaded. Functions can be overloaded by
change in number of arguments or/and change in type of arguments.
1. Example: By using different types of arguments
class MultiplyFun {
class Main {
public static void main(String[] args)
{
System.out.println(MultiplyFun.Multiply(2, 4));
System.out.println(MultiplyFun.Multiply(5.5, 6.3));
}
}
Operator Overloading: Java also provide option to overload operators. For example, we can
make the operator (‘+’) for string class to concatenate two strings. We know that this is the
addition operator whose task is to add two operands. So a single operator ‘+’ when placed
between integer operands, adds them and when placed between string operands, concatenates
them.
In java, Only “+” operator can be overloaded:
To add integers
To concatenate strings
Example:
class OperatorOVERDDN {
class Main {
public static void main(String[] args)
{
OperatorOVERDDN obj = new OperatorOVERDDN();
obj.operator(2, 3);
obj.operator("joe", "now");
}
}
Method overriding, on the other hand, occurs when a derived class has a definition for one of the
member functions of the base class. That base function is said to be overridden.
Example:
class Parent {
void Print()
{
System.out.println("parent class");
}
}
void Print()
{
System.out.println("subclass1");
}
}
void Print()
{
System.out.println("subclass2");
}
}
class TestPolymorphism3 {
public static void main(String[] args)
{
Parent a;
a = new subclass1();
a.Print();
a = new subclass2();
a.Print();
}
}