1

I am new to java and learning basic concepts. I was learning abstraction and the most basic definitions i found was : Used to hide the complexity (hide how a process will be done and show what can we do?)

Fair enough. I got a basic idea of what is abstraction. But i am not clear in few things here:

Lets see the below example:

/* File name : Employee.java */
public abstract class Employee {
   private String name;
   private String address;
   private int number;

   public Employee(String name, String address, int number) {
      System.out.println("Constructing an Employee");
      this.name = name;
      this.address = address;
      this.number = number;
   }

   public double computePay() {
     System.out.println("Inside Employee computePay");
     return 0.0;
   }

   public void mailCheck() {
      System.out.println("Mailing a check to " + this.name + " " + this.address);
   }

   public String toString() {
      return name + " " + address + " " + number;
   }

   public String getName() {
      return name;
   }

   public String getAddress() {
      return address;
   }

   public void setAddress(String newAddress) {
      address = newAddress;
   }

   public int getNumber() {
      return number;
   }
}

Salary.java

 /* File name : Salary.java */
public class Salary extends Employee {
   private double salary;   // Annual salary

   public Salary(String name, String address, int number, double salary) {
      super(name, address, number);
      setSalary(salary);
   }

   public void mailCheck() {
      System.out.println("Within mailCheck of Salary class ");
      System.out.println("Mailing check to " + getName() + " with salary " + salary);
   }

   public double getSalary() {
      return salary;
   }

   public void setSalary(double newSalary) {
      if(newSalary >= 0.0) {
         salary = newSalary;
      }
   }

   public double computePay() {
      System.out.println("Computing salary pay for " + getName());
      return salary/52;
   }

 }

Main.java

    /* File name : AbstractDemo.java */
public class AbstractDemo {

   public static void main(String [] args) {
      Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);
      Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
      System.out.println("Call mailCheck using Salary reference --");
      s.mailCheck();
      System.out.println("\n Call mailCheck using Employee reference--");
      e.mailCheck();
   }
}

My question here is , we cant even intsantiate an abstract class. So we have to extend it and overide the same method? When we override the abstract methos in the child class, the super class(abstract class method) is of no use. Also as we cant even intantiate , why cant we just write everything in one class instead of extending the abtsrcat class?

While extending the abstract class and overriding the same thing is it not a negative as the space will more for these waste abstract classes?

I know i dont have clarity and thats the reason i am confused. If anyone can clarify this (no stratight definions which are not useful for noobs like me) with explanation , i would really appreciate the time for that.

Stack Acc
  • 59
  • 1
  • 2
  • 9
  • 3
    A salary _is not_ an employee. Your class hierarchy makes no sense – SLaks Dec 18 '17 at 22:41
  • 3
    Possible duplicate of [How and when to use an abstract class](https://stackoverflow.com/questions/6007089/how-and-when-to-use-an-abstract-class) – Ousmane D. Dec 18 '17 at 22:41
  • https://stackoverflow.com/questions/29498036/what-is-the-purpose-of-abstract-classes-in-java – Ousmane D. Dec 18 '17 at 22:42
  • https://stackoverflow.com/questions/9197521/understanding-the-purpose-of-abstract-classes-in-java – Ousmane D. Dec 18 '17 at 22:42
  • 1
    @SLaks https://www.tutorialspoint.com/java/java_abstraction.htm thats the link i am following Please let me know if there is a mistake in the website. – Stack Acc Dec 18 '17 at 22:42
  • 3
    If the tutorials are making Salary extend from Employee, then ditch those tutorials and learn from a better source. There are several decent books on OOPs and Java. – Hovercraft Full Of Eels Dec 18 '17 at 22:43
  • @HovercraftFullOfEels https://www.tutorialspoint.com/java/java_abstraction.htm . i am following that link :( – Stack Acc Dec 18 '17 at 22:44
  • 2
    I saw your link, and I reiterate what I stated. Salary does not fulfill the is-a test. A Salary is not a more specific type of Employee. Rather Employees have-a Salary -- composition here, not inheritance. – Hovercraft Full Of Eels Dec 18 '17 at 22:45
  • @StackAcc The Salary class name is misleading. It acts more like "Employee with Salary" as it inherits fields from Employee such as name, address and number. – Zachary Dec 18 '17 at 22:45
  • I think the definition of "abstraction" is also misleading in that tutorial, because it implies that only abstract classes (and interfaces) can be used for "achieving abstractions". A better explanation is in [wikipedia](https://en.wikipedia.org/wiki/Abstraction_(software_engineering)#Abstraction_in_object_oriented_programming); basically _any_ classes that you design are an abstraction of the concept you are modeling. – Mick Mnemonic Dec 18 '17 at 23:15
  • you can clearly understand inheritance with HAS-A and IS-A relationship. Employee HAS-A salary so salary can be instance variable of class Employee. Employee IS-A person so Employee can inherit class Person. – 0_o Dec 18 '17 at 23:35

1 Answers1

1

Aside of bad example you use, your understanding of Abstract class is not right:

So we have to extend it and overide the same method? When we override the abstract methos in the child class, the super class(abstract class method) is of no use

First: Abstract class and Interface both stay to provide abstract methods to be overridden by extending or implementing classes.

Second: Abstract class can have common methods implementation for all extending classes - then you do not need to implement or override them over and over again. Just use them. (Note: starting from Java 8 Interface also can have default implementation for methods)

Third: if you need another than common implementation - override needed method from super class.

Forth: if in your another implementation you need to run super method - do it at any time in the implementation by calling super.methodName(...)

Vadim
  • 4,027
  • 2
  • 10
  • 26