Oops Concept




Object Oriented Programming Concept -

What is Object?

An entity that has state and behavior is known as an object

e.g., chair, bike, marker, pen, table, car, etc.

An object has three characteristics:


State: represents the data (value) of an object.


Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc.


Identity: An object identity is typically implemented via a unique ID.


The value of the ID is not visible to the external user.

An object is a real-world entity.

An object is a runtime entity.
The object is an entity which has state and behavior.
The object is an instance of a class.

What is Class?


Collection of objects is called class. It is a logical entity.


A class can also be defined as a blueprint from which you can create an individual object.


Class doesn't consume any space.


What is Constructor?

A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. 

However, constructors have no explicit return type.

Every time, an object is created using the new keyword, the default constructor of the class is called.


What is the static variable?


The static variable is used to refer to the common property of all objects.


What is the static method?


A static method belongs to the class rather than the object.

There is no need to create the object to call the static methods.
A static method can access and change the value of the static variable.

this keyword in java?


The this keyword is a reference variable that refers to the current object. 


There are the various uses of this keyword in Java.

It can be used to refer to current class properties such as instance methods, variable, constructors, etc. 

It can also be passed as an argument into the methods or constructors. 

It can also be returned from the method as the current class instance.

What is Inheritance?


When one object acquires all the properties and behaviors of a parent object, it is known as inheritance.


It provides code reusability. It is used to achieve runtime polymorphism.


What is Polymorphism?


If one task is performed in different ways, it is known as polymorphism.


For example: to convince the customer differently,

to draw something, for example, shape, triangle, rectangle, etc.

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


What is method overloading ?

Writing two or more methods in the same class in such a way that each method has same name 
but with different method signatures – is called method overloading.

What is method overriding ?


Writing two or more methods in super and sub classes such that the methods have same name 
and same signature is called method overriding.

What is Abstraction?


Hiding internal details and showing functionality is known as abstraction.

For example phone call, we don't know the internal processing.

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



What is difference between abstract class and interface?

Abstract class
Interface
1) Abstract class can have abstract and non-abstract methods.
Interface can have only abstract methods. Since Java 8, it can have default and static methods also.
2) Abstract class doesn't support multiple inheritance.
Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and non-static variables.
Interface has only static and final variables.
4) Abstract class can provide the implementation of interface.
Interface can't provide the implementation of abstract class.
5) The abstract keyword is used to declare abstract class.
The interface keyword is used to declare interface.
6) An abstract class can extend another Java class and implement multiple Java interfaces.
An interface can extend another Java interface only.
7) An abstract class can be extended using keyword "extends".
An interface can be implemented using keyword "implements".
8) A Java abstract class can have class members like private, protected, etc.
Members of a Java interface are public by default.
9)Example:
public abstract class Shape{
public abstract void draw();
}
Example:
public interface Drawable{
void draw();
}

What is Encapsulation?


Binding (or wrapping) code and data together into a single unit are known as encapsulation.


For example, a capsule, it is wrapped with different medicines.

A java class is the example of encapsulation.


Java bean is the fully encapsulated class because all the data members are private here.

What is Java instanceOf operator?

The instanceof in Java is also known as type comparison operator because it compares the instance with type. 

It returns either true or false. If we apply the instanceof operator with any variable that has a null value, it returns false. 

Example:

class Simple{  
 public static void main(String args[]){  
 Simple s=new Simple();  
 System.out.println(s instanceof Simple1);//true  
 }  
}  

Output


true

What is String in java ?


Generally, String is a sequence of characters. But in Java,
string is an object that represents a sequence of characters.

The java.lang.String class is used to create a string object.

Java StringBuffer class ?


Java StringBuffer class is used to create mutable (modifiable) string.

The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed.

What is mutable string?


A string that can be modified or changed is known as mutable string.


StringBuffer and StringBuilder classes are used for creating mutable string.

Java StringBuilder class?


Java StringBuilder class is used to create mutable (modifiable) string.

The Java StringBuilder class is same as StringBuffer class except that it is non-synchronized.
It is available since JDK 1.5.


Diffrence between  StringBuffer and StringBuilder ? 


Ans: StringBuilder is exactly same as StringBuffer (Including Methods and Constructors) except the following differences.



StringBuffer

StringBuilder

1.Every method present in StringBuffer is Synchronized.

1.No method present in StringBuilder is Non-Synchronized.

2. At a time only one thread is allow to operate on StringBuffer Object.
Hence StringBuffer object is
 Thread Safe

2.At a time multiple threads are allow to operate on StringBuilder object and hence StringBuilder object is not Thread Safe.

3. It increases waiting time of threads and hence relatively performance is low.

3.Threads are not required to wait to operate on StringBuilder object and hence relatively performance is high.

4.Introduced in 1.0 version

4. Introduced in 1.5 Version

Diffrence between String and StringBuffer?

1.Once we creates a string object we can’t perform any changes in the existing object. If we are trying to perform any changes with those changes a new object will be created. This non changeable nature is nothing but Immutability of the string object.


2. Once we creates a StringBuffer object we can perform any type of changes in the existing object. This changeable is nothing but mutability of the StringBuffer object.


String is an immutable object.

StringBuffer is a mutable object.


Difference between == operator and equals() method in java?

In general, we use == operator for reference Comparison, whereas .equals() method for content comparison.

Example: String s1=new String(“Durga”);
String s2=new String(“Durga”);
System.out.println(s1==s2);//False

System.out.println(s1.equals(s2));//True




Note:

 * .equals() method present in object class also meant for reference comparison only based on our requirement we can override for content comparison.


* In String class, all wrapper class and all collection classes .equals() method is overridden for content comparison.



Super keyword:


“this” keyword is used to represent current class object

“super” keyword is used to represent super class object.




Abstract methods:


1) The method which is having only method declaration but not method implementations such type of methods are called abstract Methods.

2) In java every abstract method must end with “ ; ”.

Example : - abstract void m1 (); ----> method declaration



Abstract class:


If any abstract method inside the class that class must be abstract.


Abstract classes are partially implemented classes hence object creation is not possible.



Interface


Interface is also one of the type of class it contains only abstract methods.

And Interfaces not alternative for abstract class it is extension for abstract classes.

The interface allows to declare only abstract methods and these methods are by default public & abstract if we are declaring or not.



Note:

If we are declaring or not each and every interface method by default public abstract. And the interfaces are by default abstract hence for the interfaces object creation is not possible.


Package:



The default package in the java programming is java.lang package.



Marker interface :



 An interface that has no members (methods and variables) is known as marker interface or tagged interface or ability interface.  In java whenever our class is implementing marker interface our class is getting some capabilities that are power of marker interface. 



Ex:- serializable , Cloneable , RandomAccess…etc



Note: - user defined empty interfaces are not a marker interfaces only, predefined empty interfaces are marker interfaces.



 Cloneable:



1) The process of creating exactly duplicate object is called cloning.

 2) We can create a duplicate object only for the cloneable classes .

3) We can create cloned object by using clone()


4) The main purpose of the cloning is to maintain backup.




Difference between Static methods and Instance methods?

  Instance method can access the instance methods and instance variables directly.

   Instance method can access static variables and static methods directly.

  Static methods can access the static variables and static methods directly.

   Static methods can’t access instance methods and instance variables directly.