R
It's not all the same.College means very little. She's useful, but having a degree doesn't guarantee anything. Most students learn to be great trout players in the school yard. In general people seek to learn fragments and think they know everything. Few are committed to the right understanding. Less are still those who have criticism to avoid talking about what they do not know. And when one learns, in general it is decoreba and you don't even understand what you're talking about.General OOP is like teenage sex, everyone says they do, but do really, few. And where you do, I don't know if you should. OOP really complicates the code a lot, so it might be better not to make OOP right even, the problem is that it needs not to do with consciousness and not by chance.Most https://pt.stackoverflow.com/q/88546/101 and each mechanism commonly used in this paradigm. Not to mention that there are different schools that define the term.These two mechanisms, along with inheritance are the basis of orientation to object, although some disagree. They have different names because they are very different.Some definitions put abstraction as another component.Even they can and are used in other paradigms jointly or separately. It's not an exclusive OOP thing.There is no relationship between them, they are completely https://pt.stackoverflow.com/q/195461/101 .Encapsulation https://pt.stackoverflow.com/q/96656/101 even could be confused with abstraction, even being different. It is about hiding the implementation details.Unlike what many find, getters and setters serve the encapsulation, but it is not encapsulation itself, that is abstraction. Encapsulation is about keeping together what is necessary to achieve the objective of the object, so all that can be done with that object must be part of it, but should not be exposed directly unless it is necessary. Actually this is even a little more complicated, because it is common that we want most operations with an object to be done outside of it, but that is another matter.Making something private is a very used mechanism, but strictly speaking is not the encapsulation itself.Encapsular is not separating the program into parts.Tragically getters and setters are exemplified as encapsulation and how people don't understand the concept well think it's just this. Interestingly those who like good practices should know the good practice that says they are not good, they just tend to be better than leaving everything public when they have no other way, https://pt.stackoverflow.com/q/133924/101 .Polymorphism https://pt.stackoverflow.com/a/153042/101 is about choosing the best algorithm for a certain need.There are some forms of polymorphism, but all, one way or another, https://pt.stackoverflow.com/q/4731/101 . The most traditional polymorphism, the dynamic or subtype is only the choice of which method to perform based on the type of object that has some relation, in general a https://pt.stackoverflow.com/q/181032/101 in the method call according to a table.O https://pt.stackoverflow.com/q/131890/101 is usually solved in compilation time according to the type of the used data and some restriction.It is still possible to have a dynamic or static polymorphism, based on https://pt.stackoverflow.com/q/39870/101 , called https://pt.stackoverflow.com/q/137185/101 or ad-hoc, that's not what we treat here, that's not what OOP does.Extra informationYou have a lot of questions about the subject right here at SOpt, just research. If you find something that wasn't asked, you can ask a new question.ExampleYou can have mistakes, I don't know or compile, it's just to show the concepts, I've made enormous simplifications. Note that you do not have such a direct relationship between methods and fields. I didn't fall in https://pt.stackoverflow.com/q/25995/101 . Java encourages polymorphism by making all methods virtual by default. See:public abstract class Conta {
private string documento; //todos aqui estão encapsulados
private string titular;
private BigDecimal saldo;
private BigDecimal limite;
private Date ultimaTroca;
public Conta(string documento, string nome, BigDecimal saldo, BigDecimal limite) {
if (!validaDocumento(documento)) throw DocumentoInvalaidoException();
this.documento = documento
this.nome = nome;
this.saldo = saldo;
this.limite = limite;
ultimaTroca = new Date();
}
private bool trocaEstaDisponivel() { //encapsula a lógica de verificação, não interessa externamente
return new Date().getTime() - ultimaTroca.getTime() < 1000 * 60 * 60 * 24 * 30 * 6;
}
private bool PodeSacar(BigDecimal saque) {
return saldo + limite - saque >= 0;
}
protected abstract bool ValidaDocumento(string documento); //encapsulado só entre a hierarquia, haverá polimorfismo
public abstract bool CadastroValido(); //é polimorfico, só o descendente terá implementação
public bool TrocaNome(string nome) { //público é o que pode fazer publicamente, o resto é detalhe interno
if (trocaEstaDisponivel()) {
this.nome = nome;
return true;
}
return false;
}
public void Deposita(BigDecimal deposito) {
saldo += deposito;
}
public bool Saca(BigDecimal saque) {
if (PodeSacar(saque)) {
saldo -= saque;
return true;
}
return false;
}
}
public class ContaJuridica : Conta {
public ContaJuridica(string documento, string nome, BigDecimal saldo, BigDecimal limite) {
Conta(documento, nome, saldo, limite);
}
@Override protected ValidaDocumento(string documento) {
return true; //só para facilitar, aqui verificaria o CNPJ
}
@Override public CadastroValido() {
return true; //aqui iria buscar na receita se o cadastro está ativou ou fazer outra coisa
}
}
//o mesmo poderia ser feito para pessoa fisica
public class main() {
public static void main (String[] args) {
ContaJuridica conta = new ContaJuridica("01456789000159", "João da Silva", 100, 50);
conta.Deposita(20);
if (!conta.trocaNome("José da Silva)) System.out.println("Não pode ficar trocando nome toda hora");
if (!conta.Saca(200)) System.out.println("Tá achando que o saco não tem fundo?");
testarConta(conta.CadastroValido());
}
public void TestaConta(Conta conta) { //note que recebe uma Conta e não ContaJuridica, então o uso será polimorfico
if (!conta.CadastroValido()) System.out.println("Sua conta precisa ser regularizada"); //chama método de ContaJuridica
}
}
https://github.com/bigown/SOpt/blob/master/Java/OOP/EncapsulationExample.java .