You are on page 1of 5

-------http://www.javatpoint.

com---------

Object-Oriented Programming is a methodology or paradigm to design a program using


classes and objects.
It simplifies the software development and maintenance by providing some concepts:
1. Object - Any entity that has state and behavior is known as an object.

2. Class - Collection of objects is called class. It is a logical entity.

3. Inheritance - When one object acquires all the properties and behaviours of
parent object i.e. known as inheritance.
It provides code reusability. It is used to achieve runtime polymorphism.

4. Polymorphism - When one task is performed by different ways i.e. known as


polymorphism.
In java, we use method overloading and method overriding to achieve polymorphism.

5. Abstraction - Hiding internal details and showing functionality is known as


abstraction.
In java, we use abstract class and interface to achieve abstraction.

6. Encapsulation - Binding (or wrapping) code and data together into a single unit
is known as encapsulation.
A java class is the example of encapsulation. Java bean is the fully encapsulated
class because all the data members are private here.

7. explain java in layman terms if I don't understand any technical stuffs like
private, class or object..
Ans- Java is a programming language which is plateform independent i.e. we can
compile code in one machine and can run in another machine with different OS.
We can develop different kind of applications using java.

8. why do we use interfaces? Why is interface concept present when something could
be achieved using classes or abstract classes?
Ans- to achieve 100% abstraction and standardization. By using classes and abstract
classes we can't achieve 100% abstraction bcoz loop holes are there.

9. tell the use of abstract classes over interfaces, and how are interfaces built
in terms of methods declared and variables within it..?
Ans- If the functionality you are creating will be useful across a wide range of
disparate objects, use an interface.
Abstract classes should be used primarily for objects that are closely related,
whereas interfaces are best suited for providing common functionality to unrelated
classes.
Interfaces can have only incomplete methods and all methods will be non-static and
public.
Variables declared inside interfaces will always be final, static and should be
initialized.

10. questions on collections like how do you do sorting using collections?


Ans- Collections.sort(List arg).

11. Giving an example, he asked me to chose which collection would I chose for that
particular scenario..
Like getwindowhandles returns set, but can I also make use of list or any other
collection? Similar questions..
Ans- No we can't use List because List allows duplicates whereas set doesn�t allow.

12. differences between overloading and overriding, what is polymorphism, what is


abstraction and write a program which shows me something which I understand about
these concepts.
Ans- overloading � developing the same method with different arg type.
Overriding- developing the methods with same method signature but different method
body. Inheritace is must for overriding.
Polymorphism- an object showing different behaviour at different stages of
development is known as Polymorphism.
Abstraction- its like hiding the implementations of the methods.
ex- class A{
void test1(){
System.out.println(�test1 of A�);
}
}
class B extends A{
void test1(){
System.out.println(�test1 of B�);
}
}
class C extends B{
void sample(A a){
System.out.println(�sample method�);
a.test1();
}
}
class D{
public static void main(String[] args){
C c1 = new C();
c1.sample(new A());
c1.sample(new B());
c1.test1();
}
}
output of this program -
A
B
B

13. write a program to check if the given input is palindrome or not by taking
input from user at runtime.
Ans- class Palindrome{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a = n;
int palindrome =0;
int r = 0;
while(n>0){
r = n%10;
n = n/10;
palindrome = palindrome*10 + r;
}
if(palindrome==a){
System.out.println(a+� number is palindrome.�);
}else{
System.out.println(a+� number is not palindrome.�);
}
}
}
14. Difference between ArrayList and LinkedList?
ArrayList and LinkedList both implements List interface and maintains insertion
order. Both are non synchronized classes.

ArrayList:
ArrayList internally uses dynamic array to store the elements.
Manipulation with ArrayList is slow because it internally uses array. If any
element is removed from the array, all the bits are shifted in memory.
ArrayList class can act as a list only because it implements List only.
ArrayList is better for storing and accessing data.
//List<String> al=new ArrayList<String>();//creating arraylist

LinkedList:
LinkedList internally uses doubly linked list to store the elements.
Manipulation with LinkedList is faster than ArrayList because it uses doubly linked
list so no bit shifting is required in memory.
LinkedList class can act as a list and queue both because it implements List and
Deque interfaces.
LinkedList is better for manipulating data.
//List<String> al2=new LinkedList<String>();//creating linkedlist

15. Difference between abstract class and interface?


Abstract class and interface both are used to achieve abstraction where we can
declare the abstract methods.
Abstract class and interface both can't be instantiated.

Abstract class:
Abstract class can have abstract and non-abstract methods.
Abstract class doesn't support multiple inheritance.
Abstract class can have final, non-final, static and non-static variables.
Abstract class can have static methods, main method and constructor.
Abstract class can provide the implementation of interface.
The abstract keyword is used to declare abstract class.

Example:
public abstract class Shape
{
public abstract void draw();
}

Interface:
Interface can have only abstract methods.
Interface supports multiple inheritance.
Interface has only static and final variables.
Interface can't have static methods, main method or constructor.
Interface can't provide the implementation of abstract class.
The interface keyword is used to declare interface.

Example:
public interface Drawable
{
void draw();
}

16. Why String is immutable type?


In java, string objects are immutable. Immutable simply means unmodifiable or
unchangeable.
Once string object is created its data or state can't be changed but a new string
object is created.
But if we explicitely assign it to the reference variable, then it points to newly
created string object.

class Testimmutablestring
{
public static void main(String args[])
{
String s="Sachin";
s.concat(" Tendulkar");//concat() method appends the string at the end

System.out.println(s);//will print Sachin because strings are immutable


objects

s=s.concat(" Tendulkar");
System.out.println(s);// will print Sachin Tendulkar
}
}

17. Why string objects are immutable in java?


Because java uses the concept of string literal. Suppose there are 5 reference
variables,all referes to one object "sachin".
If one reference variable changes the value of the object, it will be affected to
all the reference variables.
That is why string objects are immutable in java.

18. Collections in Java?


Collections in java is a framework that provides an architecture to store and
manipulate the group of objects.
All the operations that you perform on a data such as searching, sorting,
insertion, manipulation, deletion etc. can be performed by Java Collections.
Java Collection simply means a single unit of objects.
Java Collection framework provides many
interfaces (Set, List, Queue, Deque etc.) and
classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet,
LinkedHashSet, TreeSet etc).

19. What is Collection in java?


Collection represents a single unit of objects i.e. a group.

20. Difference between Comparable and Comparator?


Comparable and Comparator both are interfaces and can be used to sort collection
elements.

Comparable:
Comparable provides single sorting sequence. In other words, we can sort the
collection on the basis of single element such as id or name or price etc.
Comparable affects the original class i.e. actual class is modified.
Comparable provides compareTo() method to sort elements.
Comparable is found in java.lang package.
We can sort the list elements of Comparable type by Collections.sort(List) method.

Comparator:
Comparator provides multiple sorting sequence. In other words, we can sort the
collection on the basis of multiple elements such as id, name and price etc.
Comparator doesn't affect the original class i.e. actual class is not modified.
Comparator provides compare() method to sort elements.
Comparator is found in java.util package.
We can sort the list elements of Comparator type by
Collections.sort(List,Comparator) method.

You might also like