Вы находитесь на странице: 1из 8

What is OOP?

OOP is programming methodology based on objects, instead of just functions and


procedures. These objects are organized into classes, which allow individual objects to be
group together.
The principle concepts of OOP are:
Inheritance
Polymorphism
Abstraction
Encapsulation
What is class?
A class is a blue print from which objects are created. A class can contain fields and
methods to describe the behavior of an object.
What is Object?
In OOP paradigm, "object" refers to a particular instance of a class where the object can be
a combination of variables, functions, and data structures.
In computer science, an object is a location in memory having a value and possibly
referenced by an identifier.
What is Abstraction?
Abstraction is model of a complex system that includes only the details essential to
perspective of the viewer of the system.
A spreadsheet program used by an accountant models the books used to record
debits and credits.
What is Encapsulation?
Encapsulation is the binding of data and its associated functions in a single unit called Class.
Encapsulation is a technique used for hiding the properties and behaviors of an object and
allowing outside access only as appropriate. It prevents other objects from directly altering
or accessing the properties or methods of the encapsulated object.
What is the difference between abstraction and encapsulation?
Abstraction focuses on the outside view of an object and hides any unnecessary detail while
Encapsulation provides a protective barrier that prevents the access of code and data from
outside the class.
Abstraction solves the problem in the design side while Encapsulation is the Implementation
and infect I would say that Encapsulation in effect provides Abstraction.
What is inheritance?
Inheritance is the property which allows a Child class to inherit some properties from its
parent class. In Java this is achieved by using extends keyword. Only properties with access
modifier public and protected can be accessed in child class.
Inheritance provides re-usability of code and can be used to add additional features to an
existing class, without modifying it.
The two most common reasons to use inheritance are:
To promote code reuse
To use polymorphism
What is Polymorphism?
Polymorphism is the ability of an object to take on many forms. The most common use of
polymorphism in OOP occurs when a parent class reference is used to refer to a child class
object.
(Inheritance, Overloading and Overriding are used to achieve Polymorphism in java).

Run time polymorphism: Bank Account, savings and current (here, account class is the base
class, savings and current account are derived classes, objects of each are created at
runtime)
What is Relation between Polymorphism and Dynamic binding?
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 at 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.
What is Dynamic Binding?
Binding refers to the linking or resolving of a procedure call to the code to be executed in
response to the call. Dynamic binding means that the code associated with a given
procedure call is not known until the time of the call at run-time.
What is method overloading?
Method Overloading means to have two or more methods with same name in the same
class with different arguments. The benefit of method overloading is that it allows you to
implement methods that support the same semantic operation but differ by argument
number or type.
What is method overriding?
Method overriding occurs when sub class declares a method that has the same type
arguments as a method declared by one of its superclass. The key benefit of overriding is
the ability to define behavior thats specific to a particular subclass type.








What are the differences between method overloading and method overriding?

What is interface?
An interface is a TYPE/contract and is a collection of abstract methods. A class implements
an interface, thereby inheriting the abstract methods of the interface.
Interface cannot be instantiated
An interface does not contain any constructors.
All of the methods in an interface are abstract.
The relationship between the interface itself and the class implementing the interface is
not necessarily strong.
Interfaces are a good substitute for multiple inheritances.
What is abstract class?
A class must be declared abstract when it has one or more abstract methods. A method is
declared abstract when it has a method heading, but no body.
Abstract classes are meant to be inherited from, and when one class inherits from another
it means that there is a strong relationship between the 2 classes.
An abstract class may provide some methods with definitions
When to use abstract class and interface in Java
An abstract class is good if you think you will plan on using inheritance since it provides a
common base class implementation to derived classes.
An abstract class is also good if you want to be able to declare non-public members. In an
interface, all methods must be public.
If you think you will need to add methods in the future, then an abstract class is a better
choice. Because if you add new method headings to an interface, then all of the classes that
already implement that interface will have to be changed to implement the new methods. That
can be quite a hassle.
Interfaces are a good choice when you think that the API will not change for a while.
Interfaces are also good when you want to have something similar to multiple inheritances,
since you can implement multiple interfaces.

Overloaded Method Overridden Method
Arguments Must change Must not change
Return
type
Can change Cant change except for covariant returns
Exceptions Can change Can reduce or eliminate. Must not throw new
or broader checked exceptions
Access Can change Must not make more restrictive (can be less
restrictive)
Invocation Reference type determines
which overloaded version is
selected. Happens at compile
time.
Object type determines which method is
selected. Happens at runtime.
What is Java?
Java has Built-in support for multi-threading, socket communication, and memory
management (automatic garbage collection).
Object Oriented (OO).
Better portability than other languages across operating systems.
Supports Web based applications (Applet, Servlet, and JSP), distributed applications
(sockets, RMI, EJB etc.) and network protocols (HTTP, JRMP etc.) with the help of
extensive standardized APIs (Application Programming Interfaces).
What is JVM?
A Java virtual machine (JVM) is a process virtual machine that can execute Java bytecode.
Each Java source file is compiled into a bytecode file, which is executed by the JVM. Java
was designed to allow application programs to be built that could be run on any platform.
What is the Difference between JDK and JRE?
JRE JDK
It is an implementation of the Java Virtual
Machine* which actually executes Java programs.
It is a bundle of software that you can use to
develop Java based applications.
Java Runtime Environment is a plug-in needed for
running java programs.
Java Development Kit is needed for developing
java applications.
The JRE is smaller than the JDK so it needs less
Disk space.
The JDK needs more Disk space as it contains the
JRE along with various development tools.
The JRE can be downloaded/supported freely
from
java.com
The JDK can be downloaded/supported freely
from
oracle.com/technetwork/java/javase/downloads/
It includes the JVM , Core libraries and other
additional components to run applications and
applets written in Java.
It includes the JRE, set of API classes, Java
compiler, Webstart and additional files needed to
write Java applets and applications.
What does the static keyword mean?
The static keyword denotes that a member variable or method can be accessed, without
requiring an instantiation of the class to which it belongs.
Can you access non static variable in static context?
A static variable in Java belongs to its class and its value remains the same for all its
instances. A static variable is initialized when the class is loaded by the JVM. If your code
tries to access a non-static variable, without any instance, the compiler will complain,
because those variables are not created yet and they are not associated with any instance.
What are the Data Types supported by Java? What is Autoboxing and Unboxing?
The eight primitive data types supported by the Java programming language are: byte,
short, int, long, float, double, Boolean and char.
Autoboxing is the automatic conversion made by the Java compiler between the primitive
types and their corresponding object wrapper classes. For example, the compiler converts
an int to an Integer, a double to a Double, and so on. If the conversion goes the other way,
this operation is called unboxing.
What is exception?
An exception is an event, which occurs during the execution of a program that disrupts the
normal flow of the program's instructions and signals that something is not correct with the code
written and may give unexpected result. All the exceptions are subclasses of
java.lang.Exception.
What is error?
Errors are exceptional scenarios that are out of scope of application and its not possible to
anticipate and recover from them, for example hardware failure, JVM crash or out of
memory error.
What is difference between Error and Exception?
An error is an irrecoverable condition occurring at runtime, such as OutOfMemory error.
These JVM errors and you cannot repair them at runtime. Though error can be caught in
catch block but the execution of application will come to a halt and is not recoverable.
While exceptions are conditions that occur because of bad input etc. e.g.
FileNotFoundException will be thrown if the specified file does not exist. Or a
NullPointerException will take place if you try using a null reference. In most of the cases it
is possible to recover from an exception (probably by giving user a feedback for entering
proper values etc.)
What are the advantages of using exception handling?
Exception handling provides the following advantages over "traditional" error management
techniques:
Separating Error Handling Code from "Regular" Code.
Propagating Errors Up the Call Stack.
Grouping Error Types and Error Differentiation.
What are the types of Exceptions in Java?
There are two types of exceptions in Java, unchecked exceptions and checked exceptions.

Checked exceptions: Checked Exceptions are exceptional scenarios that we can
anticipate in a program and try to recover from it (try-catch), for example
FileNotFoundException. We should catch this exception and provide useful message to
user and log it properly for debugging purpose.
Unchecked exceptions: All Exceptions that extend the RuntimeException class are
unchecked exceptions. Class Error and its subclasses also are unchecked. Runtime
exceptions represent problems that are the result of a programming problem. Such problems
include arithmetic exceptions, such as dividing by zero.



What is difference between Checked and Unchecked Exceptions?
Checked exceptions are error scenarios that are not caused by program, for example
FileNotFoundException in reading a file that is not present, whereas Unchecked exceptions
are mostly caused by poor programming, for example NullPointerException when invoking a
method on an object reference without making sure that its not null.
Checked Exceptions should be handled in the code using try-catch block to let JRE know
about these exceptions that might be thrown from the program. Unchecked Exceptions are
not required to be handled in the program or to mention them in throws clause.
What is difference between throw and throws keyword in Java?
throws keyword is used with method signature to declare the exceptions that the method
might throw whereas throw keyword is used to disrupt the flow of program and handing
over the exception object to runtime to handle it. A throws in method signature is a
notification that the method may throw such an exception. throw statement is what actually
throw a created object under according circumstances.
What is difference between final, finally and finalize in Java?
final keyword can be used with class variables so that they cant be reassigned, with class
to avoid extending by classes and with methods to avoid overriding by subclasses, finally
keyword is used with try-catch block to provide statements that will always gets executed
even if some exception arises, usually finally is used to close resources. finalize() method
is executed by Garbage Collector before the object is destroyed, its great way to make sure
all the global resources are closed.
What is Thread?
A thread is a single sequential flow of control within a program. In the context of Java, it is
the path followed when executing a program. All Java programs have at least one thread,
known as the main thread, which is created by the JVM.
Threads are lightweight compared to processes.
Threads share the same address space and therefore can share both data and code.
Context switching between threads is usually less expensive than between processes
Cost of thread intercommunication is relatively low that that of process
intercommunication
Threads allow different tasks to be performed concurrently.
How to Create Thread in Java?
There are two ways to create thread in java;
Implement the Runnable interface (java.lang.Runnable)
By Extending the Thread class (java.lang.Thread)
Java.lang.Thread:

Java.lang.Runnable:

What is java Serialization?
Object Serialization in Java is a process used to convert Object into a binary format which
can be persisted into disk or sent over network to any other running Java virtual machine.
How to make a Java class Serializable? And what is transient variable?
Java class just needs to implements java.io.Serializable interface and JVM will take care of
serializing object in default format. transient These variables are not included in the
process of serialization.
What is difference between wait and sleep?
Sleep () is a method which is used to hold the process for few seconds or the time you
wanted but in case of wait () method thread goes in waiting state and it wont come back
automatically until we call notify () or notifyAll (). The major difference is that wait ()
releases the lock or monitor while sleep () doesnt releases any lock or monitor while
waiting. Wait is used for inter-thread communication while sleep is used to introduce pause
on execution, generally.

Вам также может понравиться