-5

Is this a correct method to achieve abstraction? As this in the abstract class

       public abstract class Employee {
            private String name, address;
            int basicSalary;
    
            public String getName(){
                return name;
            }
            
            public String getAddress(){
                return address;
            }
            
            public int getbasicSalary(){
                return basicSalary;
            }
            
            public void setName(String name){
                this.name= name;
            }
            
            public void setAddress(String address){
                this.address= address;
            }
            
            public void setBasicSalary(int basicSalary){
                this.basicSalary= basicSalary;
            }
            
            public abstract int getMonthlySalary();
        
        }
 
        

This class extends the abstract employee class

        class NormalEmployee extends Employee {
        
            public NormalEmployee() {
                // TODO Auto-generated constructor stub
            }
                    
            public void setBasicSalary(int basicSalary) {
                this.basicSalary=basicSalary;
            };
        // Method override for salarycalculation
            @Override
            public int getMonthlySalary() {
                // TODO Auto-generated method stub
                int monthlySalaryOfNormalEmployee= 1200;
                System.out.println("Normal Employee Salary: "+monthlySalaryOfNormalEmployee);
                return monthlySalaryOfNormalEmployee;
            }
        
        }
        
        public class BonusEmployee extends NormalEmployee {
            int bonusAmount;
        

This class extends normal employee class which is already inherits methods from employee class

            public BonusEmployee() {
                // TODO Auto-generated constructor stub
            }
            
                public int getBonus(){
                return bonusAmount;
                
            }
        
            public static void main(String[] args) {
                // Creating objects and calling methods
                BonusEmployee bE= new BonusEmployee();
                NormalEmployee nE= new NormalEmployee();
                bE.setBonus(1200);
                bE.setBasicSalary(1500);
                bE.getMonthlySalary();
                nE.getMonthlySalary();
            }
        
            public void setBonus(int bonusAmount){
                this.bonusAmount=bonusAmount;
            }
            
            @Override
    public int getMonthlySalary() {
        int getMonthlySalary= basicSalary + getBonus();
        System.out.println("Bonus Employee Salary: "+getMonthlySalary);
        return basicSalary;
    }
        
        }

Since its giving me expected results so don't know if the implementation is right or not!

Community
  • 1
  • 1
Anupam
  • 13
  • 4
  • `Since its giving me expected results so don't know if the implementation is right or not!` What's tripping you up here buddy? The definition of "right"? – Mad Physicist Jul 06 '16 at 20:14
  • 2
    What you use is inheritance, not abstraction. You also shouldn't repeat the `getbasicSalary` method everywhere. – zapl Jul 06 '16 at 20:16
  • @MadPhysicist, yeah you are right its the definition of Abstraction and the way to achieve it – Anupam Jul 07 '16 at 03:42
  • @zapl. Thanks. I removed getbasicSalary method from both normal and bonus employee class and still it works fine. – Anupam Jul 07 '16 at 03:50
  • 1
    If you need feedback(s) about your code I suggest ask it in [Code Review](http://codereview.stackexchange.com/). – Enzokie Jul 07 '16 at 03:57
  • On Code Review we would want to have more information (and a title) about what the code is for. If the code works as expected, you're welcome to check [How do I ask a good question](http://codereview.stackexchange.com/help/how-to-ask) and post there with the changes. – Phrancis Jul 07 '16 at 04:07

1 Answers1

1

Abstraction in any programming language is more of a concept or approach for modeling aspects of the real world as software. Abstraction is taking something which is complex in nature and representing it in software in a way that is easy to understand or use, but still models or retains the essential elements of the concept in the real world.

To write some code and then ask 'have I achieved abstraction' would depend on what it is that you are attempting to represent in code.

There's a good definition of abstraction in software development in this article here: https://en.wikipedia.org/wiki/Abstraction#In_computer_science

Kevin Hooke
  • 2,583
  • 2
  • 19
  • 33