You are on page 1of 16

A PROJECT REPORT

Submitted in partial fulfillment of the


requirements for the award of the degree of

Bachelor of Vocation in Software Development

Submitted By:

KOMAL AGARWAL
BACHELOR OF VOCATIONAL STUDIES(SOFTWARE DEVELOPMENT), 3
SEMESTER
03724818118

AMBEDKAR INSTITUTE OF TECHNOLOGY


GOVT. OF NCT OF DELHI
Affiliated to
Guru Gobind Singh Indraprastha University
Dwarka, Sector 16, New Delhi
2018-2019
CERTIFICATE
ACKNOWLEDGEMENT

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

class <class name> {


field;
method;
}

EXAMPLE

public class Dog {


String breed;
int age;
String color;

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 );
}

public static void main(String []args) {


// Following statement would create an object myPuppy
Puppy myPuppy = new Puppy( "tommy" );
}
}
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.
To achieve encapsulation in Java −
 Declare the variables of a class as private.
 Provide public setter and getter methods to modify and view the variables values.

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;

public int getAge() {


return age;
}

public String getName() {


return name;
}

public String getIdNum() {


return idNum;
}

public void setAge( int newAge) {


age = newAge;
}

public void setName(String newName) {


name = newName;
}

public void setIdNum( String newId) {


idNum = newId;
}
}

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");

System.out.print("Name : " + encap.getName() + " Age : " +


encap.getAge());
}
}

This will produce the following result −

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.

To achieve encapsulation in Java −

Declare the variables of a class as private.

Provide public setter and getter methods to modify and view the variables values.
Example

Following is an example that demonstrates how to achieve Encapsulation in


Java –

public class EncapTest {


private String name;
private String idNum;
private int age;

public int getAge() {


return age;
}

public String getName() {


return name;
}

public String getIdNum() {


return idNum;
}

public void setAge( int newAge) {


age = newAge;
}
public void setName(String newName) {
name = newName;
}

public void setIdNum( String newId) {


idNum = newId;
}
}

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 −

public class RunEncap {

public static void main(String args[]) {


EncapTest encap = new EncapTest();
encap.setName("James");
encap.setAge(20);
encap.setIdNum("12343ms");

System.out.print("Name : " + encap.getName() + " Age : " + encap.getAge());


}
}
This will produce the following result −

Output

Name : James Age : 20

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

public class Shape


{
int length;
int breadth;
}
public class Rectangle extends Shape
{
int area;
public void calcualteArea()
{
area = length*breadth;
}
public static void main(String args[])
{
Rectangle r = new Rectangle();
//Assigning values to Shape class attributes
r.length = 10;
r.breadth = 20;
//Calculate the area
r.calcualteArea();
System.out.println("The Area of rectangle of length \""
+r.length+"\" and breadth \""+r.breadth+"\" is \""+r.area+"\"");
}
}

Output

The Area of rectangle of length "10" and breadth "20" is "200"

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");
}
}

class two extends one


{
public void print_for()
{
System.out.println("for");
}
}

class three extends two


{
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 {

// Method with 2 parameter


static int Multiply(int a, int b)
{
return a * b;
}

// Method with the same name but 2 double parameter


static double Multiply(double a, double b)
{
return a * b;
}
}

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 {

void operator(String str1, String str2)


{
String s = str1 + str2;
System.out.println("Concatinated String - "+ s);
}

void operator(int a, int b)


{
int c = a + b;
System.out.println("Sum = " + c);
}
}

class Main {
public static void main(String[] args)
{
OperatorOVERDDN obj = new OperatorOVERDDN();
obj.operator(2, 3);
obj.operator("joe", "now");
}
}

RUNTIME POLYMORPHISM- Runtime polymorphism: It is also known as Dynamic Method Dispatch. It


is a process in which a function call to the overridden method is resolved at Runtime. This type of
polymorphism is achieved by Method Overriding.

 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");
}
}

class subclass1 extends Parent {

void Print()
{
System.out.println("subclass1");
}
}

class subclass2 extends Parent {

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();
}
}

You might also like