1) Abstraction & Encapsulation
2) Polymorphism
3) Inheritance
4) Abstract Class
5) Interface
Q) What is Abstraction?
A) Hiding the implementation and providing the service is called Abstraction.
Q) What is the Encapsulation?
A) Binding the data along with its related functionalities is called Encapsulation.
Ex:
class Account {
private double balance;
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
Advantages:
1) Security
2) Easy Enhancement
3) Improve Maintainability
Q) What is Polymorphism ?
A) This has been derived from Greek word. 'Poly' means Many, Morphism means 'Forms'. Totally one name with multiple forms is called Polymorphism.
Types of Polymorphism.
Ex : Method overriding.
Q) What is Method over loading .
A) Defining multiple Methods with same name, with different arguments in a class is nothing but over loading.
Ex:
public class MethodOverLoading {
public static void main(String[] args) {
MethodOverLoading overLoading= new MethodOverLoading();
overLoading.yogi(10);
overLoading.yogi(10,20);
overLoading.yogi(10,20f);
}
public void yogi(int i)
{
System.out.println("org1"+i);
}
public void yogi(int i, int j)
{
System.out.println("org2"+i+"org2"+j);
}
public void yogi(int j, double k)
{
System.out.println("org3"+j+"org4"+k);
}
public void yogi(int j, double k, float f)
{
System.out.println("org5"+j+"org6"+k);
}
}
Q) What is the Method overriding?
A) Defining a method in Sub Class with the same signature of the Super Class or defining a method in the Super Class with the same signature of the Sub Class is called a Method Overriding
Ex:
public class OverRidingEx {
public static void main(String[] args) {
Z z1= new Z();
z1.one();
X x1= new X();
x1.one();
//Z zz1= new x(); //compilation error
X xx1= new Z();
xx1.one();
}
}
class X
{
public void one()
{
System.out.println("super class");
}
}
class Z extends X
{
public void one()
{
System.out.println("sub class");
}
}
out-put
-------
sub class
super class
sub class
Q) What is Inheritance?
A) Inheritance is the concept of getting the properties of one class to another class or one object to another object is called Inheritance.
Q) Why multiple Inheritance doesn't support java.
A)
public class InheritanceEx {
public static void main(String[] args) {
}
}
class A extends B,C
{
public void M1()
{
System.out.println("class A");}
}
class B
{
public void M1()
{
System.out.println("class B");}
}
class C
{
public void M1()
{
System.out.println("class C");}
}
Explanation:
1. Multiple Inheritance is not supported in java just because of the Violation of super() keyword.
When Ever you called the method/constructor of super class at that time JVM got confused,it cant decide which method you are calling
2) Polymorphism
3) Inheritance
4) Abstract Class
5) Interface
Q) What is Abstraction?
A) Hiding the implementation and providing the service is called Abstraction.
Q) What is the Encapsulation?
A) Binding the data along with its related functionalities is called Encapsulation.
Ex:
class Account {
private double balance;
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
Advantages:
1) Security
2) Easy Enhancement
3) Improve Maintainability
Q) What is Polymorphism ?
A) This has been derived from Greek word. 'Poly' means Many, Morphism means 'Forms'. Totally one name with multiple forms is called Polymorphism.
Types of Polymorphism.
- Static Binding or Compile Time Polymorphism or Early Binding.
- Dynamic Binding or Run time Polymorphism or Late Binding.
Q) What is Method over loading .
A) Defining multiple Methods with same name, with different arguments in a class is nothing but over loading.
Ex:
public class MethodOverLoading {
public static void main(String[] args) {
MethodOverLoading overLoading= new MethodOverLoading();
overLoading.yogi(10);
overLoading.yogi(10,20);
overLoading.yogi(10,20f);
}
public void yogi(int i)
{
System.out.println("org1"+i);
}
public void yogi(int i, int j)
{
System.out.println("org2"+i+"org2"+j);
}
public void yogi(int j, double k)
{
System.out.println("org3"+j+"org4"+k);
}
public void yogi(int j, double k, float f)
{
System.out.println("org5"+j+"org6"+k);
}
}
A) Defining a method in Sub Class with the same signature of the Super Class or defining a method in the Super Class with the same signature of the Sub Class is called a Method Overriding
Ex:
public class OverRidingEx {
public static void main(String[] args) {
Z z1= new Z();
z1.one();
X x1= new X();
x1.one();
//Z zz1= new x(); //compilation error
X xx1= new Z();
xx1.one();
}
}
class X
{
public void one()
{
System.out.println("super class");
}
}
class Z extends X
{
public void one()
{
System.out.println("sub class");
}
}
out-put
-------
sub class
super class
sub class
Q) What is Inheritance?
A) Inheritance is the concept of getting the properties of one class to another class or one object to another object is called Inheritance.
Q) Why multiple Inheritance doesn't support java.
A)
public class InheritanceEx {
public static void main(String[] args) {
}
}
class A extends B,C
{
public void M1()
{
System.out.println("class A");}
}
class B
{
public void M1()
{
System.out.println("class B");}
}
class C
{
public void M1()
{
System.out.println("class C");}
}
Explanation:
1. Multiple Inheritance is not supported in java just because of the Violation of super() keyword.
When Ever you called the method/constructor of super class at that time JVM got confused,it cant decide which method you are calling
No comments:
Post a Comment