You are on page 1of 19

1. OOPS concept.

Object-oriented programming is a model that provides different types of concepts, such as


inheritance, abstraction, polymorphism, etc. These concepts aim to implement real-world entities
in programs, and they create working methods and variables to reuse them without compromising
security

What is encapsulation and abstraction? Real life example.

Encapsulation is a process of wrapping of data and methods in a single unit is called

encapsulation. Encapsulation is achieved in java language by class concept.

Combining of state and behavior in a single container is known as encapsulation. In java

language encapsulation can be achieve using class keyword, state represents declaration of

variables on attributes and behavior represents operations in terms of method.

Advantage of Encapsulation

The main advantage of using of encapsulation is to secure the data from other methods,

when we make a data private then these data only use within the class, but these data not

accessible outside the class.

Real life example of Encapsulation in Java

The common example of encapsulation is capsule. In capsule all medicine are encapsulated

in side capsule.
Benefits of encapsulation

 Provides abstraction between an object and its clients.

 Protects an object from unwanted access by clients.

 Example: A bank application forbids (restrict) a client to change an


Account's balance.

Hiding of data is known as data abstraction. In object oriented programming language this

is implemented automatically while writing the code in the form of class and object.

Real Life Example of Abstraction in Java

Abstraction shows only important things to the user and hides the internal details, for

example, when we ride a bike, we only know about how to ride bikes but can not know

about how it work? And also we do not know the internal functionality of a bike.

Data abstraction can be used to provide security for the data from the unauthorized

methods.

Note: In Java language data abstraction can achieve using class.

There are two ways to achieve abstraction in java

 Abstract class (0 to 100%)

 Interface (Achieve 100% abstraction)

Read more about Interface and Abstract class in the previous section.

Difference Between Encapsulation and Abstraction in Java

Abstraction deals with hiding the details and showing the essential things to the user

whereas encapsulation bind your data and code together as a single unit.
Encapsulation is not providing full security because we can access private member of the

class using reflection API, but in case of Abstraction we can't access static, abstract data

member of a class.

In java you can say it takes all your methods, variables and bind them together in a single

class.

Abstraction is implemented in Java using interface and abstract class while Encapsulation is

implemented using private, package-private and protected access modifier.

Advantages of Data Abstraction in Java

Here we discuss some of its benefits in order to acknowledge its significance. The benefits of

data abstraction are given below;


No Method Overloading Method Overriding
.

1) Method overloading is used to Method overriding is used to provide the


increase the readability of the specific implementation of the method that
program. is already provided by its super class.

2) Method overloading is Method overriding occurs in two


performed within class. classes that have IS-A (inheritance)
relationship.

3) In case of method In case of method overriding, parameter


overloading, parameter must be must be same.
different.

4) Method overloading is the Method overriding is the example of run


example of compile time time polymorphism.
polymorphism.

5) In java, method overloading can't Return type must be same or covariant in


be performed by changing return method overriding.
type of the method only. Return
type can be same or different in
method overloading. But you
must have to change the
parameter.

Java Method Overloading example


1. class OverloadingExample{
2. static int add(int a,int b){return a+b;}
3. static int add(int a,int b,int c){return a+b+c;}
4. }

Java Method Overriding example


1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void eat(){System.out.println("eating bread...");}
6. }

5. Collections in Java

The Java collections framework provides a set of interfaces and classes


to implement various data structures and algorithms.
For example, the LinkedList class of the collections framework provides the
implementation of the doubly-linked list data structure.

What is servlet in Java?

Simply put, a Servlet is a class that handles requests, processes them and reply back
with a response.
For example, we can use a Servlet to collect input from a user through an HTML
form, query records from a database, and create web pages dynamically.
Object means a real-world entity such as a pen, chair, table, computer, watch,
etc. Object-Oriented Programming is a methodology or paradigm to design a
program using classes and objects. It simplifies software development and maintenance
by providing some concepts:

o Object
o Class
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation
What is the difference between error and exception?

Exceptions and errors both are subclasses of Throwable class. The error indicates a problem that
mainly occurs due to the lack of system resources and our application should not catch these
types of problems. Some of the examples of errors are system crash error and out of memory
error. Errors mostly occur at runtime that's they belong to an unchecked type.
Exceptions are the problems which can occur at runtime and compile time. It mainly occurs in
the code written by the developers. Exceptions are divided into two categories such as checked
exceptions and unchecked exceptions.

Sr. No. Key Error Exception

1 Type Classified as an unchecked type Classified as checked and unchecked

2 Package It belongs to java.lang.error It belongs to java.lang.Exception

3 Recoverable/ It is irrecoverable It is recoverable


Irrecoverable

4 It can't be occur at compile time It can occur at run time compile time
both

5 Example OutOfMemoryError ,IOError NullPointerException , SqlException

Both throw and throws are the concepts of exception handing in which throw is used to
explicitly throw an exception from a method or any block of code while throws are used in the
signature of the method to indicate that this method might throw one of the listed type
exceptions.
The following are the important differences between throw and throws.

Sr. No. Key throw throws

Definition Throw is a keyword which is used Throws is a keyword used in the method
to throw an exception explicitly in signature used to declare an exception
1
the program inside a function or which might get thrown by the function
inside a block of code. while executing the code.

2 Internal Internally throw is implemented as On other hand we can declare multiple


implementation it is allowed to throw only single exceptions with throws keyword that could
Sr. No. Key throw throws

exception at a time i.e we cannot get thrown by the function where throws
throw multiple exception with keyword is used.
throw keyword.

Type of With throw keyword we can On other hand with throws keyword both
exception propagate only unchecked checked and unchecked exceptions can be
3 exception i.e checked exception declared and for the propagation checked
cannot be propagated using throw. exception must use throws keyword
followed by specific exception class name.

Syntax Syntax wise throw keyword is On other hand syntax wise throws keyword
4
followed by the instance variable. is followed by exception class names.

Declaration In order to use throw keyword we On other hand throws keyword is used with
5 should know that throw keyword is the method signature.
used within the method.

Difference between throw and throws.JavaTester.java


Live Demo

public class JavaTester{


public void checkAge(int age){
if(age<18)
throw new ArithmeticException("Not Eligible for voting");
else
System.out.println("Eligible for voting");
}
public static void main(String args[]){
JavaTester obj = new JavaTester();
obj.checkAge(13);
System.out.println("End Of Program");
}
}
Output
Exception in thread "main" java.lang.ArithmeticException:
Not Eligible for voting
at JavaTester.checkAge(JavaTester.java:4)
at JavaTester.main(JavaTester.java:10)

Example
JavaTester.java
Live Demo

public class JavaTester{


public int division(int a, int b) throws ArithmeticException{
int t = a/b;
return t;
}
public static void main(String args[]){
JavaTester obj = new JavaTester();
try{
System.out.println(obj.division(15,0));
}
catch(ArithmeticException e){
System.out.println("You shouldn't divide number by zero");
}
}
}

Output
You shouldn't divide number by zero

Inheritance with real life example.


We inherit certain properties from the class ‘Human’ such as the ability to
speak, breathe, eat, drink, etc.We can also take the example of cars. The class
‘Car’ inherits its properties from the class ‘Automobiles’ which inherits some
of its properties from another class ‘Vehicles’.

Polymorphism (code for both compile time & dynamic polymorphism)

Polymorphism means "many forms", and it occurs when we have many classes
that are related to each other by inheritance.

Like we specified in the previous chapter; Inheritance lets us inherit attributes


and methods from another class. Polymorphism uses those methods to
perform different tasks. This allows us to perform a single action in different
ways.

For example, think of a superclass called Animal that has a method


called animalSound(). Subclasses of Animals could be Pigs, Cats, Dogs, Birds -
And they also have their own implementation of an animal sound (the pig oinks,
and the cat meows, etc.):

Why you put exception.

Brief Introduction: Servlet technology is used to create a web application.


A servlet is a Java class that is used to extend the capabilities of servers that
host applications accessed by means of a request-response model. Servlets
are mainly used to extend the applications hosted by web services.

JSP is used to create web applications just like Servlet technology. A JSP is a
text document that contains two types of text: static data and dynamic data. The
static data can be expressed in any text-based format (like HTML, XML, SVG,
and WML), and the dynamic content can be expressed by JSP elements.
Difference between Servlet and JSP
The difference between Servlet and JSP is as follows:

Servlet JSP

Servlet is a java code. JSP is a HTML based code.

Writing code for servlet is harder


than JSP as it is HTML in java. JSP is easy to code as it is java in HTML.

Servlet plays a controller role in


the hasMVC approach. JSP is the view in the MVC approach for showing output.

JSP is slower than Servlet because the first step in the


hasJSP lifecycle is the translation of JSP to java code and
Servlet is faster than JSP. then compile.

Servlet can accept all protocol


requests. JSP only accepts HTTP requests.

In Servlet, we can override the


service() method. In JSP, we cannot override its service() method.

What is API?

API stands for Application Programming Interface. APIs are mechanisms that enable two
software components to communicate with each other using a set of definitions and
protocols. For example, the weather bureau's software system contains daily weather data.
The weather app on your phone “talks” to this system via APIs and shows you daily weather
updates on your phone.
What is the difference between Java and c++?

Platform independent, Java Platform dependent, should


Platform bytecode works on any operating be compiled for different
Dependency system. platforms.

It can run in any OS hence it is C++ is platform-dependent.


Portability portable. Hence it is not portable.

Java is both Compiled and C++ is a Compiled


Compilation Interpreted Language. Language.

C++ is both a procedural


Java is only an object-oriented and an object-oriented
Type programming language. programming language.

Java uses the (System C++ uses cin for input


Input-Output class): System.in for input and cout for an output
mechanism and System.out for output. operation.

Java doesn’t support goto C++ supports goto


goto Keyword Keyword keyword.

Structures and Java doesn’t support Structures C++ supports Structures


Unions and Unions. and Unions.

21. What is JDK?

Java Development Kit (JDK) is a software development environment used for developing
Java applications and applets. It includes the Java Runtime Environment (JRE), an
interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator
(Javadoc), and other tools needed in Java development\
What is the difference between c and java? Which is better?

C Java

C was developed by Dennis M. Ritchie Java was developed by James


between 1969 and 1973. Gosling in 1995.

C is a Procedural Programming Language. Java is Object-Oriented language.

C is more procedure-oriented. Java is more data-oriented.

C is a middle-level language because Java is a high-level language


binding of the gaps takes place between because translation of code takes
machine level language and high-level place into machine language using
languages. compiler or interpreter.

Java is an Interpreted language that


C is a compiled language that is it converts is in Java, the code is first
the code into machine language so that it transformed into bytecode and that
could be understood by the machine or bytecode is then executed by the
system. JVM (Java Virtual Machine).

C generally breaks down to functions. Java breaks down to Objects.

C programming language can be used for


system programming as well as Application
programming. This is not the case in Java.

C does not contain the property called


Inheritance because it does not support
OOPS, which is very useful for code
reusability. Thus C is not suited when one Java contains the property of
has to relate the things according to the Inheritance which is very useful in
real world. code reusability.

Memory allocation can be done by malloc Memory allocation can be done by a


C Java

in C new keyword in Java.

Java is a high-level language


C is a low-level language. It has difficult because translation of code takes
interpretation for the user but it has a closer place into machine language using
significance to the machine-level code. compiler or interpreter.

In C89 declaration of variables is at the


beginning of the block but in the latest
version of C that is C99 we can also
declare variables anywhere. We can declare variables anywhere.

A compiler will free up the memory


internally by calling the garbage
collector.
free is used for freeing the memory in C.

Java supports the concept of


C supports Threading. threading.

C supports pointers. Java does not supports pointers.

It is not portable. It is portable.

Call by value and call by reference is


supported in C. It only supports a call by value.

C is platform dependent. Java is a platform independent.

It supports user-based memory


management. It internally manages the memory.

C is not robust that is strict type checking


does not takes place while compile and run
time. Java is robust.
C Java

Exception handling cannot be directly


achieved in C and thus it lacks the Exception Handling is supported in
maintenance of normal flow of the program. Java.

It follows a top-down approach. Java follows a bottom-up approach.

Overloading functionality is not supported Java supports method overloading


by C. which helps in code readability.

Java does not support


C supports Preprocessors. Preprocessors.

C does not supports OOPS concept. Java supports OOPS concept.

Union and structure datatypes are Java does not supports union and
supported by C. structures.

Whereas Java does not support the


C supports the storage classes. storage classes.

It has 32 keywords. It has 50 keywords.

Go-to statements are supported in C Java does not supports go-to


language. statements.

Virtual keywords are not supported


Virtual keywords are supported by C. by Java.

Overloading functionality is not supported Java supports method overloading


by C. which helps in code readability.

Default members of Java are


Default members of C are public. private.

Data hiding is done by using static in C. Data hiding is done by using private
C Java

in Java.

A primary key is used to assure the value in the particular column is unique. The foreign
key provides the link between the two tables. Find out the major difference between primary key
and unique key.

Macro in C programming is known as the piece of code defined with the help of the #define
directive. Macros in C are very useful at multiple places to replace the piece of code with a
single value of the macro.

lass fibonacci {
static int fib(int n)
{
if (n <= 1)
return n;
return fib(n - 1) + fib(n - 2);
}

public static void main(String args[])


{
int n = 9;
System.out.println(fib(n));
}
}

Dangling pointer: A pointer pointing to a memory location that has been


deleted (or freed) is called a dangling pointer.
malloc() calloc()

It is a function that creates one It is a function that assigns more than


block of memory of a fixed one block of memory to a single
1. size. variable.

2. It only takes one argumemt It takes two arguments.

3. It is faster than calloc. It is slower than malloc()

4. It has high time efficiency It has low time efficiency

It is used to indicate memory It is used to indicate contiguous


5. allocation memory allcoation

Difference between static and dynamic memory allocation.

Memory Allocation: Memory allocation is a process by which computer


programs and services are assigned with physical or virtual memory space. The
memory allocation is done either before or at the time of program execution.
There are two types of memory allocations:

Static Memory Dynamic Memory


Allocation Allocation

1 In the static memory allocation, In the Dynamic memory


variables get allocated allocation, variables get
permanently, till the program allocated only if your
executes or function call
finishes. program unit gets active.

Static Memory Allocation is Dynamic Memory


done before program Allocation is done during
2 execution. program execution.

It uses heap for


It uses stack for managing the managing the dynamic
3 static allocation of memory allocation of memory

4 It is less efficient It is more efficient

In Dynamic Memory
Allocation, there is
memory re-usability and
In Static Memory Allocation, memory can be freed
5 there is no memory re-usability when not required

In static memory allocation, In dynamic memory


once the memory is allocated, allocation, when memory
the memory size can not is allocated the memory
6 change. size can be changed.

This allows reusing the


In this memory memory. The user can allocate
allocation scheme, more memory when required.
we cannot reuse Also, the user can release the
the unused memory when the user needs
memory. it.

In this memory allocation In this memory allocation


scheme, execution is faster scheme, execution is
than dynamic memory slower than static
8 allocation. memory allocation.

In this memory is allocated at In this memory is


9 compile time. allocated at run time.
In this allocated memory In this allocated memory
remains from start to end of the can be released at any
10 program. time during the program.

Enumeration (or enum) is a user defined data type in C. It is mainly used to


assign names to integral constants, the names make a program easy to read
and maintain.

What is a storage class in C?


We use the storage class in the C language for determining the visibility, lifetime,
initial value, and memory location of any given variable. The storage classes define
the visibility (scope) and the lifetime of any function/ variable within a C program.

The main difference between Array and Pointers is the fixed size of the memory block. When
Arrays are created the fixed size of the memory block is allocated. But with Pointers the memory
is dynamically allocated.

You might also like