Sei sulla pagina 1di 10

Assignment PGD-2101

Object Oriented Concepts Using JAVA

USOL-PGDCA

GURPREET SINGH BEDI


ROLL NO-56463
PGDCA- 2nd SEMESTER (2019-2020)
Q1: Define Dynamic Binding.

Ans: ​We know that methods are common for a set of object and instance variables are distinct
for every object. Methods are allocated their memory at the time of class declaration and
instance variables are allocated their memory at the time of object creation. In JAVA
programming language objects are allocated their memory at the time of execution using
the new operator. After allocation of memory the instance variables are binds with their
associated methods at the time of execution. This type of data binding is also called dynamic
binding or late binding or execution time binding or run time binding.

In JAVA object cannot allocate their memory statically at the time of compilation. They allocate
their memory at the time of execution. So only dynamic binding is present in JAVA. The concept
of static binding is partially implemented using the concept of static variables and static
function. Which are allocate their memory at the time of compilation and can be executed
without object. The concept of dynamic binding is present in JAVA due to JAVA supports
distributed processing. In distributed processing a large program can be decomposed to its
smaller segments and distributed among all the processors which are connected with the
server or main computer.

Q2: What are access modifiers?

Ans: ​As the name suggests access modifiers in Java helps to restrict the scope of a class,
constructor , variable , method or data member. There are four types of access modifiers
available in java:
1. Default – No keyword required
2. Private
3. Protected
4. Public
Default: When no access modifier is specified for a class, method or data member – It is said to
be having the default access modifier by default.
● The data members, class or methods which are not declared using any access modifiers
i.e. having default access modifier are accessible only within the same package.

Private:​ The private access modifier is specified using the keyword private.
● The methods or data members declared as private are accessible only within the class in
which they are declared.
● Any other class of same package will not be able to access these members.
● Top level Classes or interface can be declared as private because
1. Private means “only visible within the enclosing class”.
2. Protected means “only visible within the enclosing class and any subclasses”

GURPEEET SINGH BEDI ROLL NO-56463


Hence these modifiers in terms of application to classes, they apply only to nested classes
and not on top level classes
Protected:​ The protected access modifier is specified using the keyword protected.
● The methods or data members declared as protected are accessible within same package
or sub classes in different package.

Public:​ The public access modifier is specified using the keyword public.
● The public access modifier has the widest scope among all other access modifiers.
● Classes, methods or data members which are declared as public are accessible from
everywhere in the program. There is no restriction on the scope of a public data
members.

Q3: Mention different types of inheritance in Java​.

Ans: ​Below are the different types of inheritance which is supported by java.

● Single Inheritance
● Multiple Inheritance
● Multilevel Inheritance
● Hierarchical Inheritance
● Hybrid Inheritance

1. Single Inheritance in Java

Single Inheritance is the simple inheritance of all, When a class extends another class (Only
one class) then we call it as Single inheritance. The below diagram represents the single
inheritance in java where Class B extends only one class Class A. Here Class B will be the Sub
class and Class A will be one and only Super class.

2. Multiple Inheritance in Java

GURPEEET SINGH BEDI ROLL NO-56463


Multiple Inheritance is nothing but one class extending more than one class. Multiple
Inheritance is basically not supported by many Object Oriented Programming languages such
as Java, Small Talk, C# etc. (C++ Supports Multiple Inheritance). As the Child class has to manage
the dependency of more than one Parent class. But you can achieve multiple inheritance in Java
using Interfaces.

3. Multilevel Inheritance in Java


In Multilevel Inheritance a derived class will be inheriting a parent class and as well as the
derived class act as the parent class to other class. As seen in the below diagram. Class
B inherits the property of Class A and again Class B act as a parent for Class C. In Short Class
A parent for Class B and Class B parent for Class C.

GURPEEET SINGH BEDI ROLL NO-56463


4. Hierarchical Inheritance in Java

In Hierarchical inheritance one parent class will be inherited by many sub classes. As per the
below example Class A will be inherited by Class B, Class C and Class D. Class A will be acting as
a parent class for Class B, Class C and Class D.

5. Hybrid Inheritance in Java

Hybrid Inheritance is the combination of both Single and Multiple Inheritance. Again Hybrid
inheritance is also not directly supported in Java only through interface we can achieve this.
Flow diagram of the Hybrid inheritance will look like below. As you can Class A will be acting as
the Parent class for Class B & Class C and Class B & Class C will be acting as Parent for Class D.

GURPEEET SINGH BEDI ROLL NO-56463


Q4: What is the use of access modifiers in java?

Ans: ​Access modifiers are used for encapsulation: they allow you to arrange your code in
packages and classes, and have only an "official" public interface visible to the outside, while
hiding the implementation details (which you want to do, so that you can later change it
without telling anyone).

This is especially (only?) important, when you release code as a library for other programmers
to use. Even if the code is only used in your own program, it helps to structure larger programs
into several packages. Making everything private makes no sense unless your program consists
of only one class. It is a reasonable default, however: Until you think some other class needs to
call it, make the method private.

The default modifier in Java (if nothing is specified) is package-protected. This allows the
method or field to be accessed from your own related code (i.e. code in the same package),
while still hiding it from anyone else. It also is a reasonable default, or the natural upgrade path
from private: Until code outside of the package needs to call it, make the method
package-protected. The more visible levels are protected (subclasses can see it), and public
(everyone can see it). Before making something public or protected, think about the interface
carefully, because it gets difficult to change it later.

GURPEEET SINGH BEDI ROLL NO-56463


Q5: Explain the use of using arrays in java.

Ans: ​Java provides a data structure, the array, which stores a fixed-size sequential collection of
elements of the same type. An array is used to store a collection of data, but it is often more
useful to think of an array as a collection of variables of the same type. Instead of declaring
individual variables, such as number0, number1, ..., and number99, you declare one array
variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent
individual variables.

Q6: What is the difference between string and StringBuffer?


Ans: There are many differences between String and StringBuffer. A list of differences between
String and StringBuffer are given below:

SNo. String StringBuffer

1) String class is immutable. StringBuffer class is mutable.

2) String is slow and consumes more StringBuffer is fast and consumes less
memory when you concat too many memory when you cancat strings.
strings because every time it creates
new instance.

3) String class overrides the equals() StringBuffer class doesn't override the
method of Object class. So you can equals() method of Object class.
compare the contents of two strings
by equals() method.

Q7: How is the interface different from abstract class?


Ans:

1. Main difference is methods of a Java interface are implicitly abstract and cannot have
implementations. A Java abstract class can have instance methods that implements a
default behavior.
2. Variables declared in a Java interface is by default final. An abstract class may contain
non-final variables.
3. Members of a Java interface are public by default. A Java abstract class can have the
usual flavors of class members like private, protected, etc..

GURPEEET SINGH BEDI ROLL NO-56463


4. Java interface should be implemented using keyword “implements”; A Java abstract
class should be extended using keyword “extends”.
5. An interface can extend another Java interface only, an abstract class can extend
another Java class and implement multiple Java interfaces.
6. A Java class can implement multiple interfaces but it can extend only one abstract class.
7. Interface is absolutely abstract and cannot be instantiated; A Java abstract class also
cannot be instantiated, but can be invoked if a main() exists.
8. In comparison with java abstract classes, java interfaces are slow as it requires extra
indirection.

Q8: What is a package? What are the benefits of using a package?


Ans: ​A java package is a group of similar types of classes, interfaces and sub-packages. Package
in java can be categorized in two form, built-in package and user-defined package. There are
many built-in packages such as java, lang, awt, java x, swing, net, io, util, sql etc. Here, we will
have the detailed learning of creating and using user-defined packages.

Benefits of Java Package

1) Java package is used to categorize the classes and interfaces so that they can be easily
maintained.

2) Java package provides access protection.

3) Java package removes naming collision.

4) Packages provide reusability of code.

5) To bundle classes and interfaces.

6) We can crate our own Package or extend already available Package.

Q9: Describe the life cycle of a thread?

Ans: Life Cycle of a Thread

A thread goes through various stages in its life cycle. For example, a thread is born, started,
runs, and then dies. The following diagram shows the complete life cycle of a thread.

GURPEEET SINGH BEDI ROLL NO-56463


Following are the stages of the life cycle −
● New​ − A new thread begins its life cycle in the new state. It remains in this state until
the program starts the thread. It is also referred to as a ​born thread​.
● Runnable​ − After a newly born thread is started, the thread becomes runnable. A
thread in this state is considered to be executing its task.
● Waiting​ − Sometimes, a thread transitions to the waiting state while the thread waits
for another thread to perform a task. A thread transitions back to the runnable state
only when another thread signals the waiting thread to continue executing.
● Timed Waiting​ − A runnable thread can enter the timed waiting state for a specified
interval of time. A thread in this state transitions back to the runnable state when that
time interval expires or when the event it is waiting for occurs.
● Terminated (Dead)​ − A runnable thread enters the terminated state when it completes
its task or otherwise terminates.

Q10: What is an applet? What are the different types of applet?


Ans: ​Applets are small Java programs that are primarily used in internet computing
they can be transported over the internet form one computer to another and run
using the applet viewer (or) any web browser that supports Java.An applet can
perform arithmetic operations graphics, play sounds , accept user input, create
animation, and play interactive games.
Local and Remote Applets :

GURPEEET SINGH BEDI ROLL NO-56463


we can embed applets in to web pages in to ways one, we can write our applets and
embed them in to web pages. Second, we can download an applet from a remote
computer system and then embed it in to a web page.
An applet developed locally and stored in a local system is known as local applet when a
web page is trying to find a local applet it does not need to use the internet and
therefore the local system and locates and loads the specified applets.
A remote applet is that which is developed by some one else and stored on a remote
computer connected to the internet we can download the remote applet on to our
system via at the internet and run it.
In order to locate and load a remote applet, we must known the applets address on the
web. This address is known as uniform resource locator (URL).

GURPEEET SINGH BEDI ROLL NO-56463

Potrebbero piacerti anche