Encapsulation and abstraction with an example in java

I know that my question will be set for closing right after publication, but let me tell you that I was looking for a site for a clear difference in Java examples. Please do not rush to close the question.

I wrote the following:

public class Bank {
public String name="XXX Bank";
private int noOfCust;

public int getNoOfCust() {
 return getNoOfCusts() + 2;
}
public void setNoOfCust(int noOfCust) {
if(noOfCust > 0)
{
this.noOfCust = noOfCust;
}
}

private int getNoOfCusts()
{
noOfCust = 100;
return noOfCust;
}
}

Here "noOfCust - hidden data". I do not consider part of the implementation by introducing private getNoOfCusts (). Therefore, I believe that this is encapsulation, because I linked all the details in the class and hid the data too.

Question 1: If it is strong encapsulation? If not, how can I improve it so that it is heavily encapsulated?

Question 2: Abstraction means completely hiding the implementation. So what I did above is an abstraction?

- , , , .

P.S: , . .

+5
5

Abstraction. . . , eyeColor, skinColor. , Person, , , . . .

() () . , .

wiki: - , ++, Object Pascal Java, - virtual ( ++) ( Java). .

+10
  • encapsulation , object . .

  • , getter/setter combo - . , private public setter() getter().

, , . , , :

public class Bank{
    private int noOfCustomers;
    private String name;


    public String getName(){
        return this.name;
    }

    public void setName(String name){
        this.name = name;
    }

    public int getNoOfCustomers(){
        return this.noOfCustomers;
    }

    public void setNoOfCustomers(int noOfCustomers){
        this.noOfCustomers = noOfCustomers;
    }

    /**
    * This method is to illustrate that it is perfectly valid to have
    * multiple types of setter methods
    */
    public void setNoOfCustomers(String noOfCustomers){
        try{
            this.noOfCustomers = Integer.parseInt(noOfCustomers);
        }catch(Exception exe){
            //Handle exceptions
        }
    }
}

abstraction: - , , . , Bank abstraction, , - - . , .

, abstraction, , File Java. . , , , , private .

, , , , , .

+3

: , . , .

: , . getter/setter . ( , - ). ( ) , , ( , , "" ). , , .

, , , . "" , , .. , , , .

+2

, .

. , , . , int getter setter ( getter, , ). String name ; , , , , .

- , . Java , , , , . abstract, , ; ( ), , .

Java, , Baseball Ball:

public abstract class Ball {
    // this class cannot be instantiated
    public abstract double getRadius();

    public double getVolume() {
        return (4.0 / 3) * Math.PI * Math.pow(getRadius(), 3);
    }
}

public class Baseball extends Ball {
    // this class can be instantiated
    @Override
    public double getRadius() {
        return 3.0;
    }
}
0

Encapsulation in java happens automatically through objects and methods. You do not need to take care of this. However, if you want, you can use various hiding methods to increase your complexity. This way, others will not be able to decrypt your code. You can use recursive functions and complex methods and add unnecessary codes. Strong encapsulation!

-1
source

All Articles