java - making a class with abstraction is also encapsulation? -


encapsulation said wrapping of data , method , hidding functionality(method , instance variable) not needed outside of object

my question making variable private , public encapsulation ? or making class abstraction encapsulation ?

for example: have switch(electic switch) class doing on or off make switch class have used abstraction , encapsulated switch class using abstraction can map motor or bulb or electric instrument

public class switch {     private boolean isoff = true;     private iswitchlistener listener;     public switch(iswitchlistener listener) {         this.listener = listener;     }     public void trigger() {         isoff = !isoff;             if(isoff) {             listener.off();         } else {             listener.on();         }     } }  public class bulb implements iswitchlistener {      @override     public void on() {         // todo auto-generated method stub         system.out.println("bulb glittering");     }      @override     public void off() {         // todo auto-generated method stub         system.out.println("bulb not glittering");     }  }  public interface iswitchlistener {     public void on();     public void off(); }  public class executor {     public static void main(string[] args) {         // todo auto-generated method stub         switch swt = new switch(new bulb());          swt.trigger();               } } 

if not using abstraction here , have class below

public class switch {     private boolean isoff = true;     public void trigger() {          isoff = !isoff;         bulb b =new bulb();         if(isoff) {             b.off();         } else {             b.on();         }     } } 

when want map motor switch need change class bleow

public class switch {     private boolean isoff = true;     public void trigger() {          isoff = !isoff;         bulb b =new bulb();         if(isoff) {             b.off();         } else {             b.on();         }     } }   public class motor {     public void on() {         // todo auto-generated method stub         system.out.println("motor rotating");     }      public void off() {         // todo auto-generated method stub         system.out.println("motor getting off rotate");     } } 

in general, no, abstraction , encapsulation 2 different things. specifically, abstraction means removing detail unnecessary intended purpose of code or model. example, if write program calculate how paint need house, model of house needs include surface area, doesn't need include address, or size of yard. encapsulation means hiding internal workings of object or module, such coupling between objects can controlled , readily changed. if there no encapsulation, clients of object might directly reference internal elements, mean have change clients if modified object.

regarding code example, think better solution create interface "switchable" , have motor , bulb implement it. so:

public interface switchable {    void on();    void off(); }  public class motor implements switchable {     public void on() {         system.out.println("motor rotating");     }      public void off() {         system.out.println("motor getting off rotate");     } }  public class switch {     private boolean isoff = true;     private switchable switchable;      public switch(switchable switchable) {         this.switchable = switchable;     }      public void trigger() {         isoff = !isoff;         if(isoff) {             switchable.off();         } else {             switchable.on();         }     } } 

hope helps.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -