Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
32 views

Java Module

Uploaded by

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

Java Module

Uploaded by

abhiramtp71
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

JAVA PROGRAMMING

MODULE 1
1. Difference between procedural programming and object-oriented
programming.

Procedural Oriented Programming Object Oriented Programming

In procedural programming, program is In object-oriented programming, program


divided into small parts called functions. is divided into small parts called objects.

Procedural programming follows top-down Object oriented programming


approach. follows bottom-up approach.

Object oriented programming have access


There is no access specifier in procedural specifiers like private, public, protected
programming. etc.

Adding new data and function is not easy. Adding new data and function is easy.

Procedural programming does not have any


proper way for hiding data so it is less Object oriented programming provides
secure. data hiding so it is more secure.

In procedural programming, overloading is Overloading is possible in object-oriented


not possible. programming.

In procedural programming, function is more In object-oriented programming, data is


important than data. more important than function.

Procedural programming is based on unreal Object oriented programming is based


world. on real world.

Examples: C, FORTRAN, Pascal, Basic etc. Examples: C++, Java, Python, C# etc.

2. How java differs from C++?


• C++ is platform-dependent whereas Java is platform-independent.
• C++ is mainly used for system programming.Java is mainly used for
application programming. It is widely used in window, web-based, enterprise
and mobile applications.
• Just like C++, Java is also statistically typed object-oriented programming
language with a similar with an incompatible syntax. Java has extensive
documentation as well which is known as Javadoc.
• C++ uses pointers while Java does not involve pointers. Java uses the concept
of ‘restricted pointers’.
• A C++ program is run and the compilation is done using its compiler. The
compiler in C++ converts source code into machine level language which
makes C++ a platform-dependent language. In Java, java source code is first
converted into bytecode at the time of compilation. This bytecode is then
interpreted by the interpreter and runtime to produce the output that makes
Java a platform-independent language.
• C++ does not provide built-in support for threads whereas Java support
threads implicitly.
• C++ supports multiple inheritance.Java doesn't support multiple inheritance
through class. It can be achieved by interfaces in java.
• Java is much like C++ but does not involve complex concepts like structures,
operator overloading, pointers, templates, unions, etc. Also, Java doesn’t have
support for conditional compile (#ifdef/#ifdef type).
• Every entity is an object in java except fundamental types. Java has a single
root hierarchy as everything emerged from java.lang.Object.

3. What is OOPS?
Object-Oriented Programming System (OOPs) is a programming concept that works
on the principles of abstraction, encapsulation, inheritance, and polymorphism. It
allows users to create objects they want and create methods to handle those objects.
The basic concept of OOPs is to create objects, re-use them throughout the program,
and manipulate these objects to get results.

4. Explain the basic concept of object-oriented programming.


Or List the basic principles of Object Orientation.
Object-Oriented Programming or OOPs refers to languages that use objects in
programming. Object-oriented programming aims to implement real-world entities
like inheritance, hiding, polymorphism etc. in programming. The main aim of OOP is
to bind together the data and the functions that operate on them so that no other part
of the code can access this data except that function.
OOPs Concepts:
• Class
• Object
• Method
• Encapsulation
• Abstraction
• Polymorphism
• Inheritance
• Message Passing
5. Explain class
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:
Modifiers: A class can be public or has default access (Refer this for details).
Class name: The name should begin with a initial letter (capitalized by convention).
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.
Interfaces (if any): A comma-separated list of interfaces implemented by the class, if
any, proceeded by the keyword implements. A class can implement more than one
interface.
Body: The class body surrounded by braces, { }.
6. Explain object
Object: It is a basic unit of Object Oriented Programming and represents the real life
entities. A typical Java program creates many objects, which as you know, interact by
invoking methods. An object consists of:
State: It is represented by attributes of an object. It also reflects the properties of an
object.
Behaviour: It is represented by methods of an object. It also reflects the response of an
object with other objects.
Identity: It gives a unique name to an object and enables one object to interact with
other objects.
Example of an object: dog

Method: A method is a collection of statements that perform some specific task and
return result to the caller. A method can perform some specific task without returning
anything. Methods allow us to reuse the code without retyping the code. In Java,
every method must be part of some class which is different from languages like C,
C++ and Python.
Methods are time savers and help us to reuse the code without retyping the code.
Method Declaration
In general, method declarations have six components:
Access Modifier: Defines access type of the method i.e. from where it can be accessed
in your application. In Java, there are 4 types of the access specifiers.
public: accessible in all class in your application.
protected: accessible within the package in which it is defined and in its
subclass(es)(including subclasses declared outside the package)
private: accessible only within the class in which it is defined.
default (declared/defined without using any modifier): accessible within same class
and package within which its class is defined.
The return type: The data type of the value returned by the method or void if does not
return a value.
Method Name: the rules for field names apply to method names as well, but the
convention is a little different.
Parameter list: Comma separated list of the input parameters are defined, preceded
with their data type, within the enclosed parenthesis. If there are no parameters, you
must use empty parentheses ().
Exception list: The exceptions you expect by the method can throw, you can specify
these exception(s).
Method body: it is enclosed between braces. The code you need to be executed to
perform your intended operations.

7. Define Encapsulation

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. Another
way to think about encapsulation is, it is a protective shield that prevents the data
from being accessed by the code outside this shield.
Technically in encapsulation, the variables or data of a class is hidden from any other
class and can be accessed only through any member function of own 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.
Encapsulation can be achieved by declaring all the variables in the class as private and
writing public methods in the class to set and get the values of variables.
8. Define Abstraction
Abstraction: Data Abstraction is the property by virtue of which only the essential
details are displayed to the user. The trivial or the non-essentials units are not
displayed to the user. Ex: A car is viewed as a car rather than its individual
components.
Data Abstraction may also be defined as the process of identifying only the required
characteristics of an object ignoring the irrelevant details. The properties and
behaviours of an object differentiate it from other objects of similar type and also help
in classifying/grouping the objects.
Consider a real-life example of a man driving a car. The man only knows that
pressing the accelerators will increase the speed of car or applying brakes will stop the
car but he does not know about how on pressing the accelerator the speed is actually
increasing, he does not know about the inner mechanism of the car or the
implementation of accelerator, brakes etc. in the car. This is what abstraction is.

9. Explain Inheritance?

Inheritance: Inheritance is an important pillar of OOP(Object Oriented Programming).


It is the mechanism in java by which one class is allow to inherit the features(fields
and methods) of another class.
Important terminology:
Super Class: The class whose features are inherited is known as superclass(or a base
class or a parent class).
Sub Class: The class that inherits the other class is known as subclass(or a derived
class, extended class, or child class). The subclass can add its own fields and methods
in addition to the superclass fields and methods.
Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to
create a new class and there is already a class that includes some of the code that we
want, we can derive our new class from the existing class. By doing this, we are
reusing the fields and methods of the existing class.
The keyword used for inheritance is extends.
Syntax:

class derived-class extends base-class


{
//methods and fields
}
10. Explain Polymorphism
Polymorphism:The word ‘polymorphism’ literally means ‘a state of having many
shapes’ or ‘the capacity to take on different forms’. When applied to object-oriented
programming languages like Java, it describes a language’s ability to process objects
of various types and classes through a single, uniform interface.
Polymorphism in Java has two types: Compile time polymorphism (static binding)
and Runtime polymorphism (dynamic binding). Method overloading is an example of
static polymorphism, while method overriding is an example of dynamic
polymorphism.
An important example of polymorphism is how a parent class refers to a child class
object. In fact, any object that satisfies more than one IS-A relationship is
polymorphic in nature.
For instance, let’s consider a class Animal and let Cat be a subclass of Animal. So,
any cat IS animal. Here, Cat satisfies the IS-A relationship for its own type as well as
its super class Animal.
11. Explain Message Passing
Message Passing: 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.
12.Explain Static Polymorphism?
In Java, static polymorphism is achieved through method overloading. Method
overloading means there are several methods present in a class having the same name
but different types/order/number of parameters.

At compile time, Java knows which method to invoke by checking the method
signatures. So, this is called compile time polymorphism or static binding. The
concept will be clear from the following example:

classDemoOverload{
publicint add(int x, int y){ //method 1
returnx+y;
}

publicint add(int x, int y, int z){ //method 2


returnx+y+z;
}

publicint add(double x, int y){ //method 3


return (int)x+y;
}

publicint add(int x, double y){ //method 4


return x+(int)y;
}
}

class Test{
public static void main(String[] args){
DemoOverload demo=new DemoOverload();
System.out.println(demo.add(2,3)); //method 1 called
System.out.println(demo.add(2,3,4)); //method 2 called
System.out.println(demo.add(2,3.4)); //method 4 called
System.out.println(demo.add(2.5,3)); //method 3 called
}
}
In the above example, there are four versions of add methods. The first method takes
two parameters while the second one takes three. For the third and fourth methods,
there is a change of order of parameters. The compiler looks at the method signature
and decides which method to invoke for a particular method call at compile time.

13.Explain Dynamic Polymorphism?


Suppose a subclass overrides a particular method of the superclass. Let’s say we
create an object of the subclass and assign it to the superclass reference. Now, if we
call the overridden method on the superclass reference then the subclass version of the
method will be called.

Have a look at the following example.

class Vehicle{
public void move(){
System.out.println(“Vehicles can move!!”);
}
}

classMotorBike extends Vehicle{


public void move(){
System.out.println(“MotorBike can move and accelerate too!!”);
}
}

class Test{
public static void main(String[] args){
Vehicle vh=new MotorBike();
vh.move(); // prints MotorBike can move and accelerate too!!
vh=new Vehicle();
vh.move(); // prints Vehicles can move!!
}
}
It should be noted that in the first call to move(), the reference type is Vehicle and the
object being referenced is MotorBike. So, when a call to move() is made, Java waits
until runtime to determine which object is actually being pointed to by the reference.
In this case, the object is of the class MotorBike. So, the move() method of MotorBike
class will be called. In the second call to move(), the object is of the class Vehicle. So,
the move() method of Vehicle will be called.
As the method to call is determined at runtime, this is called dynamic binding or late
binding.

14.Give an account on dynamic method dispatch in java.


Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to
an overridden method is resolved at runtime rather than compile-time.
In this process, an overridden method is called through the reference variable of a
superclass. The determination of the method to be called is based on the object being
referred to by the reference variable.
When Parent class reference variable refers to Child class object, it is known as
Upcasting. In Java this can be done and is helpful in scenarios where multiple child
classes extend one parent class. In those cases we can create a parent class reference
and assign child class objects to it.

15.Difference between Static binding and Dynamic binding in Java?


• Static binding in Java occurs during compile time while dynamic binding
occurs during runtime.
• Static binding uses type (Class) information for binding while dynamic
binding uses instance of class (Object) to resolve calling of method at run-
time.
• Overloaded methods are bonded using static binding while overridden
methods are bonded using dynamic binding at runtime.
• In simpler terms, Static binding means when the type of object which is
invoking the method is determined at compile time by the compiler
whileDynamic binding means when the type of object which is invoking the
method is determined at run time by the compiler.

16.What are benefits of OOP?


• Code reusability new objects can be derived from old objects, allowing for
improvement and refinement of the code at each stage and also preserving
parts of the code for other programs. This is used to develop many class
libraries using class codes that have already been written, for example,
Microsoft Foundation Classes (MFC).
• Code Modularity Everything in OOP is an object; these objects can be
interchanged or removed to meet the users’ needs.
• Easier maintenance Inheritance usually reduces maintenance because of the
‘domino effect it has on derived classes when a change is made in a base class.
• Design stability once a stable base class has been developed, the new classes
that are derived may have fewer less errors and bugs.
• Improved communication between developers and users Objects can be
broken down into real life entities; hence it is easier it communicate ideas.
• Seamless transition from design to implementation this is mainly because
communications are improved.
17. Explain the features of object orientation.
Attributes
State
Identity
Operation
Behaviour
An object is a single unit having both data and the processes that operate on that data.
For example, in object oriented programming language like C++, the data and
functions are bundled together as a self-contained unit called an object. An object is
an entity which has some properties and behaviour associated with it. Objects are the
basic run time entities in an object oriented system. Programming problems are
analysed in terms of objects. The main purpose of using objects is following.
They correspond to the real life entities.
They provide interactions with the real world.
They provide practical approach for the implementation of the solution.
All the objects have a state, behaviour and identity.
Attributes: They describe information about the object.
State: It is represented by attributes of an object. It also reflects the properties of an
object.
Behaviour: It specifies what the object can do. It defines the operation performed on
objects. It is represented by methods of an object. It also reflects the response of an
object with other objects.
Identity: It gives a unique name to an object and enables one object to interact with
other objects.
Operation:An Operation is a service that can be requested from any object of the
Class to affect behavior. An Operation can either be a command or a question. A
question should never change the state of the object only a command can. The
outcome of the Operation depends on the current state of the object.

Exampleofanobject:dog

18.What is Dynamic Binding?

Dynamic binding or late binding is the mechanism a computer program waits until
runtime to bind the name of a method called to an actual subroutine. It is an
alternative to early binding or static binding where this process is performed at
compile-time. Dynamic binding is more expensive computationally, but it has the
advantage of being more likely to avoid version conflicts when binding functions of a
linked library.The ability to perform dynamic binding is a common characteristic of
high-level languages such as C++, Java, and LISP..

You might also like