Hide and show example using Apex in Salesforce

Sample Code:

Visualforce page:

<apex:page controller="HideAndShow" sidebar="false" showHeader="false" >
<apex:form >
<p style="font-weight:800; color:#4C0000;">&nbsp;&nbsp;&nbsp;Click the buttons below to experiment Hide and Show.</p>
    <apex:pageBlock title="Block A" rendered="{!abool}">
        This is Block A.<br/><br/>       
    </apex:pageBlock>
   
    <apex:pageBlock title="Block B" rendered="{!bbool}">
        This is Block B.<br/><br/>
    </apex:pageBlock>   
   
    <apex:pageBlock >
        <apex:commandButton value="Show A" action="{!showA}" rendered="{!sabool}" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<apex:commandButton value="Show B" action="{!showB}" rendered="{!sbbool}" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<apex:commandButton value="Hide A" action="{!hideA}" rendered="{!habool}" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<apex:commandButton value="Hide B" action="{!hideB}" rendered="{!hbbool}" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<apex:commandButton value="Show A and B" action="{!showAB}" rendered="{!sabbool}"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<apex:commandButton value="Hide A and B" action="{!hideAB}" rendered="{!habbool}"/>
    </apex:pageBlock>
</apex:form>   
</apex:page>


Apex Controller:


public class HideAndShow
{
    public Boolean abool {get;set;}
    public Boolean bbool {get;set;}
   
    public Boolean sabool {get;set;}
    public Boolean sbbool {get;set;}
    public Boolean habool {get;set;}
    public Boolean hbbool {get;set;}
    public Boolean sabbool {get;set;}
    public Boolean habbool {get;set;}
   
    public HideAndShow()
    {
        sabool = true;
        sbbool = true;
        sabbool = true;
        abool = false;
        bbool = false;
    }
   
    public void showA()
    {
        abool = true;
        check();
    }
   
    public void showB()
    {
        bbool = true;
        check();
    }  
    
    public void hideA()
    {
        abool = false;
        check();
    }
   
    public void hideB()
    {
        bbool = false;
        check();
    }  
   
    public void showAB()
    {
        abool = true;
        bbool = true;
        check();
    }  
   
    public void hideAB()
    {
        abool = false;
        bbool = false;
        check();
    }   
   
    public void check()
    {
        if(abool == true && bbool == false)
        {
            sabool = false;
            sbbool = true;
            habool = true;
            hbbool = false;
            sabbool = true;
            habbool = false;
        }
        else if(abool == false && bbool == true)
        {
            sabool = true;
            sbbool = false;
            habool = false;
            hbbool = true;
            sabbool = true;
            habbool = false;
        }
        else if(abool == true && bbool == true)
        {
            sabool = false;
            sbbool = false;
            habool = true;
            hbbool = true;
            habbool = true;
            sabbool = false;

        }
        else
        {
            sabool = true;
            sbbool = true;
            habool = false;
            hbbool = false;
            sabbool = true;
            habbool = false;
        }
    }   
}


Output: