it's briefly explain the 4 major principles that make a language object-oriented:
Encapsulation, Data Abstraction, Polymorphism and Inheritence.
Encapsulation
Encapsulation hides the details of that implementation. An accessor is a method that is used to ask an object about itself. In OOP, these are usually in the form of properties, which have, under normal conditions, a get method, which is an accessor method. However, accessor methods are not restricted to properties and can be
any public method that gives information about the state of the object.
while hiding the implementation of exactly how the data gets modified. Mutilators are commonly another portion of the property discussed above, except this time its the set method that lets the caller modify the member data behind the scenes.
class Loan{
private int duration; //private variables examples of encapsulation
private String loan;
private String borrower;
private String salary;
private int duration; //private variables examples of encapsulation
private String loan;
private String borrower;
private String salary;
//public constructor can break encapsulation instead use factory method
private Loan(int duration, String loan, String borrower, String salary){
this.duration = duration;
this.loan = loan;
this.borrower = borrower;
this.salary = salary;
}
private Loan(int duration, String loan, String borrower, String salary){
this.duration = duration;
this.loan = loan;
this.borrower = borrower;
this.salary = salary;
}
//no argument consustructor omitted here
// create loan can encapsulate loan creation logic
public Loan createLoan(String loanType){
//processing based on loan type and than returning loan object
//processing based on loan type and than returning loan object
return loan;
}
}
Why Encapsulation:
1. Encapsulated Code is more flexible and easy to change with new requirements.
2. Encapsulation in Java makes unit testing easy.
3. Encapsulation in Java allows you to control who can access what.
4. Encapsulation also helps to write immutable class in Java which are a good choice in multi-threading environment.
5. Encapsulation reduce coupling of modules and increase cohesion inside a module because all piece of one thing are encapsulated in one place.
6. Encapsulation allows you to change one part of code without affecting other part of code.
Abstraction
Data abstraction and encapsulation are closely tied together, because a simple definition of data abstraction is the development of classes, objects, types in terms of their interfaces and functionality, instead of their implementation details. Abstraction denotes a model, a view, or some other focused representation for an actual item.
Its the development of a software object to represent an object we can find in the real world. Encapsulation hides the details of that implementation.
Why Abstraction:
1) Use abstraction if you know something needs to be in class but implementation of that varies.
2) In Java you cannot create instance of abstract class , its compiler error.
3) Abstract is a keyword in java.
4) a class automatically becomes abstract class when any of its method declared as abstract.
5) Abstract method doesn't have method body.
6) Variable cannot be made abstract, its only behavior or methods which would be abstract.
7) If a class extends an abstract class or interface it has to provide implementation to all its abstract method to be a concrete class. Alternatively this class can also be abstract.
7) If a class extends an abstract class or interface it has to provide implementation to all its abstract method to be a concrete class. Alternatively this class can also be abstract.
Inheritance
Objects can relate to each other with either a “has a”, “uses a” or an “is a” relationship. “Is a” is the inheritance way of object relationship.
So, take a library, for example. A library lends more than just books, it also lends magazines, audio-cassettes and microfilm. On some level, all of these items can be treated the same: All four types represent assets of the library that can be loaned out to people.
However, even though the 4 types can be viewed as the same, they are not identical. A book has an ISBN and a magazine does not. And audio-cassette has a play length and microfilm cannot be checked out overnight.Each of these library’s assets should be represented by its own class definition.
Without inheritance though, each class must independently implement the characteristics that are common to all loanable assets.
Example will mentioned with Polymorphism Example.
Polymorphism
Polymorphism means one name, many forms.
also is the ability to create a variable, a function, or an object that has more than one form. In principle, polymorphism can arise in other computing contexts and shares important similarities with the concept of degeneracy in biology.
There are 2 basic types of polymorphism.
- Overridding, also called run-time polymorphism,
- Overloading, which is referred to as compile-time polymorphism.
Method overloading means writing two are or more methods in the same class by using same method name, but passing the parameters is different.
Method overriding means we use the method names in the different classes,that means parent class method is used in the child class.
Example:
public abstract class Human
{
public abstract void printGender();
}
public class Male extends Human
{
@Override
public void printGender()
{
{
System.out.println("man");
}
}
public class Female extends Human
{
@Override
public void printGender()
{
{
System.out.println("women");
}
}
public class boy extends male
{
@Override
public void printGender()
{
{
System.out.println("teanager");
}
public void printGender(String name)
{
System.out.println(name +"teenager");
}
}
- Male is Inherit Human.
- Boy .printGender() method is override the method printGender in Man Class.
- printGender(name)/printGender() is overloaded.
References:
- http://stackoverflow.com/questions/154577/polymorphism-vs-overriding-vs-overloading
- http://codebetter.com/raymondlewallen/2005/07/19/4-major-principles-of-object-oriented-programming/
- http://javarevisited.blogspot.com/2012/03/what-is-encapsulation-in-java-and-oops.html
- http://javarevisited.blogspot.com/2010/10/abstraction-in-java.html
No comments:
Post a Comment