0% found this document useful (0 votes)
67 views

Experiment No 1 Aim: Software Used: Theory:: Object Oriented Programming

The document discusses object-oriented programming concepts like abstraction, encapsulation, inheritance, and polymorphism. It then provides an introduction to Java, describing how it is a platform-independent language and how the Java Virtual Machine works. Finally, it outlines two experiments: [1] writing a program to demonstrate an array of objects, explaining arrays, objects, and how to create an array of objects; [2] writing a program to implement default and parameterized constructors in Java.

Uploaded by

Jay Gade
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Experiment No 1 Aim: Software Used: Theory:: Object Oriented Programming

The document discusses object-oriented programming concepts like abstraction, encapsulation, inheritance, and polymorphism. It then provides an introduction to Java, describing how it is a platform-independent language and how the Java Virtual Machine works. Finally, it outlines two experiments: [1] writing a program to demonstrate an array of objects, explaining arrays, objects, and how to create an array of objects; [2] writing a program to implement default and parameterized constructors in Java.

Uploaded by

Jay Gade
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

EXPERIMENT NO 1

AIM:

Software used: Netbeans

THEORY:

Object oriented programming


Object Oriented Programming is a method of implementation in which programs are
organized as cooperative collection of objects, each of which represents an instance of a class,
and whose classes are all members of a hierarchy of classes united via inheritance
relationships.

Four principles of Object Oriented Programming are

Abstraction
Encapsulation
Inheritance
Polymorphism

Abstraction

Abstraction denotes the essential characteristics of an object that distinguish it from all other
kinds of objects and thus provide crisply defined conceptual boundaries, relative to the
perspective of the viewer.

Encapsulation

Encapsulation is the process of compartmentalizing the elements of an abstraction that


constitute its structure and behavior ; encapsulation serves to separate the contractual
interface of an abstraction and its implementation.

* Hides the implementation details of a class.


* Forces the user to use an interface to access data
* Makes the code more maintainable.

Inheritance

Inheritance is the process by which one object acquires the properties of another object.

Polymorphism

Polymorphism is the existence of the classes or methods in different forms or single name
denoting different implementations.
Introduction to java :

Java is an object-oriented programming language developed by Sun Microsystems


and released in 1991.
Java was originally developed by James Gosling at Sun Microsystems (which has
since merge into Oracle Corporation).
Java programs are platform independent which means they can be run on any
operating system with any type of processor as long as the Java interpreter is available
on that system.
Java code that runs on one platform does not need to be recompiled to run on another
platform, its called write once, run anywhere(WORA).
Java Virtual Machine (JVM) executes Java code, but is written in platform specific
languages such as C/C++/ASM etc. JVM is not written in Java and hence cannot be
platform independent and Java interpreter is actually a part of JVM.

Earlier java was only used to design and program small computing devices but later adopted
as one of the platform independent programming language and now according to Sun, 3
billion devices run java.

Java is one of the most important programming language in todays IT industries.

JSP Java is used to create web applications like PHP and ASP, JSP(Java Server
Pages) used with normal HTML tags, which helps to create dynamic web pages.
Applets This is another type of Java program that used within a web page to add
many new features to a web browser.
J2EE The software Java 2 Enterprise Edition are used by various companies to
transfer data based on XML structured documents between one another.
JavaBeans This is something like Visual Basic, a reusable software component that
can be easily assemble to create some new and advanced application.
Mobile Besides the above technology, Java is also used in mobile devices, many
kind of games and services built-in Java. Today, all leading mobile service provider
like Nokia, Siemens, Vodafone are using Java technology.

Types of Java Applications

1. Web Application Java is used to create server-side web applications. Currently,


servlet, jsp, struts, jsf etc. technologies are used.
2. Standalone Application It is also known as desktop application or window-based
application. An application that we need to install on every machine or server such as
media player, antivirus etc. AWT and Swing are used in java for creating standalone
applications.
3. Enterprise Application An application that is distributed in nature, such as banking
applications etc. It has the advantage of high level security, load balancing and
clustering. In java, EJB is used for creating enterprise applications.
4. Mobile Application Java is used to create application softwares for mobile devices.
Currently Java ME is used for creating applications for small devices, and also Java is
programming language for Google Android application development.

Java Virtual Machine


Java was designed with a concept of write once and run everywhere. Java Virtual
Machine plays the central role in this concept. The JVM is the environment in which Java
programs execute. It is a software that is implemented on top of real hardware and operating
system. When the source code (.java files) is compiled, it is translated into byte codes and
then placed into (.class) files. The JVM executes these bytecodes. So Java byte codes can be
thought of as the machine language of the JVM. A JVM can either interpret the bytecode one
instruction at a time or the bytecode can be compiled further for the real microprocessor using
what is called a just-in-time compiler. The JVM must be implemented on a particular
platform before compiled programs can run on that platform.

The JVM performs following main tasks:

Loads code
Verifies code
Executes code
Provides runtime environment
Experiment No. 2

Aim :-Write a program to demonstrate the use of Array of Objects.

Theory :-

1) What is Array?
Normally, array is a collection of similar type of elements that have contiguous
memory location. Java array is an object the contains elements of similar data type. It is a
data structure where we store similar elements. We can store only fixed set of elements in a
java array. Array in java is index based, first element of the array is stored at 0 index.

2) What are Objects in Java?

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 you compare the software
object with a real-world object, they have very similar characteristics.

An object is nothing but a self-contained component which consists of methods and


properties to make a particular type of data useful. Object determines the behaviour of the
class. When you send a message to an object, you are asking the object to invoke or execute
one of its methods.From a programming point of view, an object can be a data structure, a
variable or a function. It has a memory location allocated. The object is designed as class
hierarchies.

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.

3) Creating Objects in Java :-


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.

Eg) Here is an example of creating an object of type Rectangle

Rectangle R1; //Declare Object R1 of class Rectangle


R1= new Rectangle(); //Initialize an object
OR
Rectangle R1= new Rectangle();

4) Array Of Objects

An array of objects is created just like an array of primitive type data items in the following
way :

Student[] studentArray = new Student[7];

The above statement creates the array which can hold references to seven Student
objects. It doesn't create the Student objects themselves. They have to be created separately
using the constructor of the Student class. The studentArray contains seven memory spaces in
which the address of seven Student objects may be stored. If we try to access the Student
objects even before creating them, run time errors would occur. For instance, the following
statement throws a NullPointerException during runtime which indicates that studentArray[0]
isn't yet pointing to a Student object.

studentArray[0].marks = 100;

The Student objects have to be instantiated using the constructor of the Student class
and their references should be assigned to the array elements in the following way.

studentArray[0] = new Student();

In this way, we create the other Student objects also. If each of the Student objects have
to be created using a different constructor, we use a statement similar to the above several
times. However, in this particular case, we may use a for loop since all Student objects are
created with the same default constructor.

for ( int i=0; i<studentArray.length; i++) {


studentArray[i]=new Student();
The above for loop creates seven Student objects and assigns their reference to the array
elements.
In the case of array of objects, when we pass an array element to a method, the object is
susceptible to changes. This is because the element being passed is also a reference type item.
This differs from the situation when we have an int array.
EXPERIMENT NO 3

AIM: Write a program to implement default and paramaterized constructor.

THEORY:

Constructor in java is a special type of method that is used to initialize the object. Java
constructor is invoked at the time of object creation. It constructs the values i.e. provides data
for the object that is why it is known as constructor.

There are basically two rules defined for the constructor.

1. Constructor name must be same as its class name


2. Constructor must have no explicit return type

There are two types of constructors:

1. Default constructor (no-arg constructor)


2. Parameterized constructor

Default constructor:

A constructor that have no parameter is known as default constructor.

Default constructor refers to a constructor that is automatically created by compiler in


the absence of explicit constructors.
You can also call a constructor without parameters as default constructor because all
of its class instance variables are set to default values.
If you dont implement any constructor in your class, the Java compiler inserts default
constructor into your code on your behalf. You will not see the default constructor in
your source code(the .java file) as it is inserted during compilation and present in the
bytecode.
Syntax of default constructor:

<class_name>(){}

Paramaterized constructor:

A constructor that have parameters is known as parameterized constructor.

Parameterized constructor is used to provide different values to the distinct objects.

Constructor Overloading in Java

Constructor overloading is a technique in Java in which a class can have any number of
constructors that differ in parameter lists.The compiler differentiates these constructors by
taking into account the number of parameters in the list and their type.

Java Copy Constructor

There is no copy constructor in java. But, we can copy the values of one object to another
like copy constructor in C++.

There are many ways to copy the values of one object into another in java. They are:

By constructor
By assigning the values of one object into another
By clone() method of Object class

Super Keyword

What is super in Java?


Super is a keyword of Java which refers to the immediate parent of a class and is used inside
the sub class method definition for calling a method defined in super class. A super class
having methods as private cannot be called. Only the methods which are public and
protected can be called by the keyword super. It is also used by class constructors to invoke
constructors of its parent class.

Syntax:

super.<method-name>();

Usage of super class

Super variables refer to the variable of variable of parent class.


Super() invokes the constructor of immediate parent class.
Super refers to the method of parent class.

Example to use the super keyword with a method:

public class Class1{

public String String_Method(){

return
super.overriden_String_Method();
}
EXPERIMENT NO 4

AIM: Write a program to implement types of inheritance.

THEORY:

Inheritance in java is a mechanism in which one object acquires all the properties and
behaviors of parent object.

The idea behind inheritance in java is that you can create new classes that are built upon
existing classes. When you inherit from an existing class, you can reuse methods and fields of
parent class, and you can add new methods and fields also.

Inheritance represents the IS-A relationship, also known as parent-child relationship.

For Method Overriding (so runtime polymorphism can be achieved).


For Code Reusability.

Syntax of Java Inheritance

1. class Subclass-name extends Superclass-name


2. {
3. //methods and fields
4. }

The extends keyword indicates that you are making a new class that derives from an existing
class. The meaning of "extends" is to increase the functionality.In the terminology of Java, a
class which is inherited is called parent or super class and the new class is called child or
subclass.Types of inheritance in java

1. Single Inheritance

2. Multilevel Inheritance

3. Hierarchical Inheritance

4. Hybrid Inheritance

Single Inheritance

Single inheritance is damn easy to understand. When a class extends another one class only
then we call it a single inheritance. The below flow diagram shows that class B extends only
one class which is A. Here A is a parent class of B and B would be a child class of A.
Multilevel Inheritance

Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a
derived class, thereby making this derived class the base class for the new class. As you can
see in below flow diagram C is subclass or child class of B and B is a child class of A.
Hierarchical Inheritance

In such kind of inheritance one class is inherited by many sub classes. In below example
class B,C and D inherits the same class A. A is parent class (or base class) of B,C & D.

Hybrid Inheritance

In simple terms you can say that Hybrid inheritance is a combination of Single and Multiple
inheritance. A typical flow diagram would look like below. A hybrid inheritance can be
achieved in the java in a same way as multiple inheritance can be!! Using interfaces. yes you
heard it right. By using interfaces you can have multiple as well as hybrid inheritance in
Java.
EXPERIMENT NO 5

AIM:

THEORY:

Interface in Java
An interface is a blueprint of class. It is a java core part and a way to achieve data abstraction
in Java along with abstract class. Since multiple inheritance is not allowed in Java, interface is
only way to implement multiple inheritance. At in basic level interface in java is a keyword
but that time it is an object oriented term to define contracts and abstraction, This contract is
followed by any implementation of Interface in Java.

Key Point of Interface in JAVA:

You can fully abstract a class using keyword interface.


While declaring interface in a class there is no need of keyword abstract .
All methods in an interface are implicitly public.
Interface is not a class defining an interface is similar to defining a class, but they are two
different concepts. A class define the attributes and behaviors of an object. An interface
contains behaviors that a class implements.
We have to define all the abstract methods of the interface in the class.

Defining Interfaces:

[Access-specifier] interface interface-name

Access-specifier return-type method-name(parameter-list);

final type var1=value;

}
Where, Access-specifier is either public or it is not given.When no access specifier is used, it
results into default access specifier and if interface has default access specifier then it is only
available to other members of the same package.When it is declared as public, the interface
can be used by any other code of other package.

Interface-Name: name of an interface, it can be any valid identifier.

The methods which are declared having no bodies they end with a semicolon after the
parameter list. Actually they are abstract methods;Any class that includes an interface must
implement all of the methods. Variables can be declared inside interface declarations.They
are implicitly final and static, means they can not be changed by implementing it in a class.

Java Implementing interfaces :

Once an interface has been defined, one or more classes can implement that interface.To
implement an interface, include the implements clause in a class definition, and then create
the methods declared by the interface.
The general form of a class that includes the implements clause looks like this:

Access-specifier class classname [extends superclass] [implements interface, [, interface..]]

// class body

If a class implements from more than one interface, names are separated by comma.If a class
implements two interfaces that declare the same method, then the same method will be used
by clients of either interface.

Java Extending interfaces :


One interface can inherit another by use of the keyword extends. The syntax is the same as
for inheriting classes.When a class implements an interface that inherits another interface,
It must provide implementation of all methods defined within the interface inheritance.
Any class that implements an interface must implement all methods defined by that interface,
including any that inherited from other interfaces.

You might also like