Java Module
Java Module
MODULE 1
1. Difference between procedural programming and object-oriented
programming.
Adding new data and function is not easy. Adding new data and function is easy.
Examples: C, FORTRAN, Pascal, Basic etc. Examples: C++, Java, Python, C# etc.
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.
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
9. Explain Inheritance?
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;
}
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.
class Vehicle{
public void move(){
System.out.println(“Vehicles can move!!”);
}
}
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.
Exampleofanobject:dog
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..