1) what is encapsulation ?
Encapsulation JAVA's PIE (polymorphosim, Inheritance, Encapsulation) concept, to protect the data.
What is protection of data in encapsulation?
Class person {
int salary;
// getters
public void setSalary(int salary) {
if(salary < 0) {
//throw exception
}
}
}
person p = new person();
p.setSalary(10000);
But you are creating the person p object, through p object you are setting the value of salary, But how are you protecting data here ?
While entering the salary data, in the setSalary method we had put some constraints of not allowing the < 0 values, we are protecting the salary allows only integer values > 0.
2) Inside enum class why can't declare abstract methods ?
enum Biryani {
abstract void recipes() ;
}
1) if you declare any abstract method, then you will make class also abstract. these abstract method implemented by child class
*** Any child class can't extend enum class, because enum class is final.
enum class final, final and abstract combination will not work.
*** Inside enum only instance and static methods not abstract methods.
3) Difference between enum, Enum, Enumeration ?
enum: enum type just like class type. defined group of named constants.
class ramesh {
},
enum ramesh {
}
Enum: It's a class present in java.lang package which act as a base class for all java enums
Enumeration: It's an interface in java.lang, which can be used for retrieving objects
from collection one by one
Encapsulation JAVA's PIE (polymorphosim, Inheritance, Encapsulation) concept, to protect the data.
What is protection of data in encapsulation?
Class person {
int salary;
// getters
public void setSalary(int salary) {
if(salary < 0) {
//throw exception
}
}
}
person p = new person();
p.setSalary(10000);
But you are creating the person p object, through p object you are setting the value of salary, But how are you protecting data here ?
While entering the salary data, in the setSalary method we had put some constraints of not allowing the < 0 values, we are protecting the salary allows only integer values > 0.
2) Inside enum class why can't declare abstract methods ?
enum Biryani {
abstract void recipes() ;
}
1) if you declare any abstract method, then you will make class also abstract. these abstract method implemented by child class
*** Any child class can't extend enum class, because enum class is final.
enum class final, final and abstract combination will not work.
*** Inside enum only instance and static methods not abstract methods.
3) Difference between enum, Enum, Enumeration ?
enum: enum type just like class type. defined group of named constants.
class ramesh {
},
enum ramesh {
}
Enum: It's a class present in java.lang package which act as a base class for all java enums
Enumeration: It's an interface in java.lang, which can be used for retrieving objects
from collection one by one
No comments:
Post a Comment