Abstraction in Java? -


Today I have heard from my friend that encapsulation is not only hiding information but also acquiring intangibility. How does it get?

  public class employee {name of private string; Private Ent ID; Public Zero Setname (string name) {this.name = name; } Public string getName () {return name; }}   

The above example gets the encapsulation where I allow the class to access my public method rather than the private members, but where does the picture get an abstract picture? Can anyone explain me clearly on the abstract?

Two different things, information hidden and intangible.

It is possible to hide information, but it is something different, for example, use of your code

  public class employee {Private string name; Private Ent ID; Public Zero Setname (string name) {this.name = name; } Public string getName () {return name; }}   

ID area is actually hidden; it allows someone to handle AIDS in a way that is decoded with the rest of the program. Your name field is actually hidden as you do not reach the namespace directly, but the code in getName and setName does.

Once you hide the structure of the data from the rest of the code, forcing the access through methods, it is possible to implement many replaceable implementations of an item. For example, a employee is a conceptual type of person , so that you can overwrite above:

  public interface person { Public abstract string getName (); } Public class employee employee {name of private string; Private Ent ID; Public Zero Setname (string name) {this.name = name; } Public string getName () {return name; }}   

Now your code can deal with employee with person . After rewriting the rest of the code, it does not deal with person to deal with employee , then you can apply the other type of person Take advantage of S and non-employee specific tasks which are now person work.

  public customer device person {name of private string; Private integer passport; Public string getName () {return name; }}   

Then search for a person from a regular search, as long as it only searches for both employees s in the index person objects And customers . The reason for this is that the code dealing with the person objects is actually working with a high level of intangibles, which can be found in the employee and customers Share the objects.

During handling objects at an abstract level, names of methods are shared across the abstract; However, the actual code object that is executed depends on the uncertain type of object. In other words, if you ask a person (who is an employee) getName () then this will respond with the employee .getName () function, while one Customer will respond with a Customer.getName () function. Since the code is going on code getName () is person s, he does not know what kind of person will have to handle it, but a clear change in behavior (copy of selection - The right block of code on the basis of the object) still happens This phenomenon is known as polymorphicim, and if you are killing these concepts before, you will hear a lot of polymorphism as a word.

An example of polymorphic behavior:

  public interfaces animal {public essence string mesound (;); } Public class cow applied animal {public string makesound () {return "Mu mo!";; }} Public square dog tools animal {public string makesound () {return "ruff ruff!"; }} Public Squared Sheep Applied {Public String MakeSound () {Return "Ba Ba!" ;; }} // This Clauval Polymorphic Behavior Public Category Form {Public Stable Zero Main (String [] Args) {ArrayList & gt; Animals & gt; Animal = new arreelist & gt; Animals & gt; (); Animals.add (new cow ()); Animals.add (New Sheep ()); Animals.add (new dog ()); (Animal animal: animals) {// This is where the polymorphism occurs / every animal will make a different sound / / because the makesound method is getting //, hide the class under animal rescues / println ( Animal.makeSound ()); }}}   

Expected Output:

  Moo Moo! Ba Baif Ruff!   

Although we have never changed the classes clearly, and we never explicitly change the methods, it was the bond of clear method that was changing for the clear subclass, which That's something that supports polymorphism.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -