Tuesday, 17 March 2015
Q 01) What is OOP What Are The Concepts In It ?
It is a programming paradigm used to combine data and
procedures in a package called “OBJECTS”.
The object oriented programming language directly represents
the real life objects like a car, jeep, customer, account, etc..
There are 3 main concepts of OOPS they are:
- Encapsulation
- Inheritance
- Polymorphism
These features of Object Oriented programming languages
( ie, PIE ) which make Java Powerful .And they are the Pillars of OOPL.
PIE : Short form to remember 3 concepts of OOPL.
Where P - Polymorphism.
I - Inheritance.
E - Encapsulation.
Q 02) How does OOP approach improve software development
The key benefits are:
- Reuse of previous work.
- Real mapping to problem domain.
- Modular Architecture.
- Increased quality and reduced development time.
Q 03) Polymorphism
Poly – many.
morph – forms.
(In Greek )
In a
nutshell, Polymorphism is a bottom-up method call .The key benefit of
polymorphism is that it is very easy to add new classes of derived objects
without breaking the calling code. That uses the polymorphic classes or
interfaces. When you send a message to an object even though you don’t know
what specific type it is and the right thing happens that is called as " Polymorphism ".
There are 2 types of polymorphisms: -
- Method Overloading (Static Binding or Compile time Polymorphism )
- Method Overriding (Dynamic Binding or Runtime Polymorphism)
Q 04) Over Riding
- Over Riding is also known as Runtime – polymorphism/Dynamic – Binding/Late-Binding
- In a class hierarchy, when a method in the sub class has the same name and same type signature as that of the method in the superclass, Then the method in the subclass is said to be over ridden by the super class.
- Method overriding occurs only when the Names and their types (Data Types) of two methods are identical.
- This shows the implementation of INHERITANCE.
- It is a mechanism where calls to the overridden method is resolved at runtime (hence Runtime Polymorphism).
Why Over
ridden Methods?
- Runtime polymorphism is supported.
- Concept of polymorphism is established.
- Rules of Over Riding:
- Same name and same type signature.
- Final methods cannot be overridden.
- Private methods doesn’t participate, because these methods are not visible to the child class.
package
org.javanoobs.overriding;
// This is the super Class.
public class Vehicle {
public void move(){
System.out.println(" Vehicle can
move.. ");
}
}
package
org.javanoobs.overriding;
// This class is a subclass.
public class MotorBike extends Vehicle {
public void move(){
System.out.println(" Motorbikes
can move and Accelerate !!! Wroom... ");
}
}
package
org.javanoobs.overriding;
// This is the main method..
public class MainMethod {
public static void main(String args[]){
Vehicle
vh = new MotorBike(); // First object
vh.move();
vh
= new Vehicle(); // Second object
vh.move();
}
}
OUT PUT:
Motorbikes can move and Accelerate !!!
Wroom...
Vehicle can move..Q 05) Over Loading
Two or more methods in the same
class with “ SAME NAME ” But their parameter declarations are different, This
is called as overloading.
·
It shows the implementation of polymorphism.
·
Static Polymorphism is achieved by method overloading.
Two Rules of Overloading:
- Argument list must differ.
- Return types cannot be different.
Example Program of Over Loading :
package
org.javanoobs.overloading;
public class CalculateSum {
void findSum (int a, int b){
int twoSum = a+b;
System.out.println(" Result of
int a + int b : " +twoSum);
}
void findsum (int a , double b){
double twoSum = a +b;
System.out.println(" Result of
int a + double b : "+twoSum);
}
void findSum (double a, double b){
double twoSum = a + b;
System.out.println("Result of
double a + double b : " +twoSum);
}
public static void main (String
args[]){
CalculateSum cs = new CalculateSum();
cs.findSum(10, 20);
cs.findsum(10, 20.22);
cs.findSum(22.34, 32.98);
}
}
OUTPUT:
Result of int a + int b : 30
Result of int a + double b : 30.22
Result of double a + double b : 55.31999999999999
Q 06) Over Riding VS Over Loading
OverRiding/ Dynamic Binding/
Runtime polymorphism
|
OverLoading/ Static Binding/
Compile Time Polymorphism
|
|
01)
|
Over – Riding happens at RUN –
TIME
I,e The binding of over-riding method call to its definition happens at
Runtime.
|
Over – Loading happens at COMPILE – TIME.
I,e The binding of over-loading method
call to its definition happens at Compile time.
|
02)
|
Static methods cannot be over - ridden, even if
you declare static method in the child class, It has nothing to do with the
same method of parent.
|
Static methods can be over - loaded, Which means a class can have more
than one static method of same name.
|
03)
|
Over – riding requires two
classes a Base and a child class.
|
Over – loading is done in the same
class.
|
04)
|
Dynamic Binding is used for Over - ridden/
Over-riding methods.
|
Static Binding is used for Over- loading methods.
|
OverRiding/ DynamicBinding/
Runtime
polymorphism
|
OverLoading/Static
Binding/
Compile
Time Polymorphism
|
|
05)
|
PERFORMANCE
:
Over – Riding is slow compared to
Over - loading, because the binding of Over –
riding methods are done at
run –
time.
|
PERFORMANCE
:
Over – loading is better and faster
compared to Over-riding.
|
06)
|
Private
final methods cannot
be Over – ridden.
|
Private
final methods can
be Over – Loaded.
|
07)
|
Return
Type of Over –
riding methods can be different from Over-ridden methods.
|
Return
Type of Over –
loading should be same for all methods.
|
08)
|
Argument
– List should be
same in Over – riding
|
Argument
– List should be
different while Over – loading.
|
Q 07) Inheritance
It is a mechanism by which a specific object acquires attributes and behaviors of more general objects
(Or)
Inheritance is the inclusion of state (Variables) and behavior (methods_ of a base/super class in a derived/sub class, So that they are accessible in the subclass.
- The key benefit of inheritance is that it provides the formal mechanism for “Code Reuse”.
Single
Inheritance:
- Single Inheritance is the property in which a subclass inherits certain features or properties of the super class.
- In java inheritance is achieved by using the key word “Extends”.
Multi - Level Inheritance (Multiple Inheritance):
- When a child class inherits multiple properties form multiple classes, it is known as multiple Inheritance.
- But java does not allow us to extend multiple classes, however this problem is overcome by the implementation of multiple interfaces.
Q 08) Encapsulation
Encapsulation is the methodology of hiding certain data
elements of the implementation but providing a public Interface to the client
or the user.
(Or)
It’s basically about hiding the state of an object, with the
help of modifiers like, private, public, protected…
(Or)
Data Encapsulation is wrapping up of information (ie..
attributes and Methods ) within a class .
Eg : A class
Benefits of
Encapsulation:
- The fields of a class can be made read-only or write-only.
- A class can have total control over what is stored in its fields.
- The users of a class do not know how the class stores its data.
- Specifying members a private can hide the variables and methods.
- Objects should hid e their inner workings from the outside view.
- Good Encapsulation improves code modularity by preventing objects interacting with each other in an unexpected way.
Example Program of Encapsulation …
package
org.javanoobs.encapsulation;
public class Encapsulation {
private String name;
private int id;
// This is the
setter method for variable name
public void setName( String
newName){
this.name = newName;
}
// This is the
getter method for varaible name.
public String getName (){
return name;
}
//This is the
setter method for variable id
public void setId ( int newId){
this.id = newId;
}
//this is the
getter method for variable id
public int getId(){
return id;
}
}
package
org.javanoobs.encapsulation;
public class Main {
public static void main(String[] args)
{
Encapsulation
enc = new Encapsulation();// object of
encapsulation class.
enc.setId(10000);
enc.setName(" Danny ");
System.out.println(" Id : " +enc.getId() + " Name :
"
+enc.getName());
}
}
OUTPUT
:
Id :
10000 Name : Danny
Q 09) Abstraction
Abstraction is a process of hiding
the implementation details and showing only functionality to the user.
Abstraction means showing only
important things to the user and hiding internal details.
Eg : Sending SMS/TEXT MSG ( The process involved ).
There are two types of abstraction :
- Abstract Class (0 – 100%)
- Interfaces (100%)
Q 10) Abstract Class
Abstract Class:
- Creation of a super class that only defines a generalized form (features of different sub classes) that will be shared by all of the sub classes, where in the subclass will fill up rest of the details.
- An abstract class is a class that is declared with an “ abstract “ keyword.
- An abstract class in never " Instantiated " meaning an instance of abstract class cannot be created.
- It can have both abstract and non-abstract methods (methods without body and concrete methods).
- Abstract Classes can have Constructors, member variables and normal (concrete) methods.
- It is used to provide Abstraction.
- Abstract Classes are not Interfaces.
Syntax of
Abstract Class:
abstract
class class – name {
}
Abstract
Method:
·
A method that is declared without any body within an
abstract class is known as Abstract method.
·
The method can be defined by its sub class.
·
Abstract methods can never be final or static.
·
Any class that extends abstract class must implement
all the abstract methods declared by the super class or make the child class
abstract.
Abstract
Method Syntax:
abstract
return_type method_name();
//No
definition.
Example Program of Abstract Class:
package
org.javanoobs.abstractdemo;
public abstract class A {
// This is the
abstract method..
abstract void callMe();
//This is the
normal or concrete method..
public void normal(){
System.out.println(" This is the
normal or concrete method.. ");
}
}
package
org.javanoobs.abstractdemo;
public class B extends A {
void callMe(){
System.out.println(" This is the
abstract call me method !!! ");
}
public static void main(String[] args)
{
B
b = new B();
b.callMe();
b.normal();
}
}
OUTPUT :
This is the abstract call me method
!!!
This is the normal or concrete
method..
Subscribe to:
Posts (Atom)