Tính trừu tượng trong Java | Codelearn

  • TuanLQ7
  • Easy
  • 100 Points

Task

Create 3 classes Person, Employee and Customer with the following information:

Person class

  • Person is a subtraction class used to store general information about human objects.
  • name and address are private attibutes about name and address of an object.
  • Person(name: String, address: String) is a constructor with parameter which is used to initializing values for attributes.
  • display() is an abstract method used to display information of the object.

Employee class

  • Employee is the class inheriting from the abstract class Person
  • salary attribute stores information about employees' salary.
  • Employee(name: String, address: String, salary: int) is the constructor used to initializing values for attibutes.
  • display() is the method overriding from the absstract attributes, it is used to display information of employees on the screen. For example, if name = "Trung", address = "HN", salary = 3300. when we call the display() method, the program produces the following information:
    Employee name: Trung
    Employee address: HN
    Employee salary: 3300​

Customer class

  • Customer is the class inheriting from the abstract class Person
  • balance is the attribute of storing information about the balance in a customer's account.
  • Customer(name: String, address: String, balance: int) is the constructor that is used to initializing values for attributes.
  • display() is the method overriding from the abstract class. This attribute is used to display information of employee on the screen. For example, if name = "Linh", address = "BN", balance = 10400, when we call display() method, the program produces the following result:
    Customer name: Linh
    Customer address: BN
    Customer balance: 10400

Note: In a class diagram, if a class name is italic, it is an abstract class, if the method name is italic, it is an abstract method.

The program to test the above 3 classes:

public class Entry {
	public static void main(String[] args) {
		Person person1 = new Employee("Trung", "HN", 3300);
		Person person2 = new Customer("Linh", "BN", 10400);
		person1.display();
		person2.display();
	}
}

When the above code is compiled and executed, it produces the following result:

Employee name: Trung
Employee address: HN
Employee salary: 3300
Customer name: Linh
Customer address: BN
Customer balance: 10400

Theory

In this article, you will learn about the last concept in object oriented programming, which is abstraction.

Abstraction

Abstraction is a property that focuses only on the features of the object and hides unnecessary information. It helps you focus more on the feature rather than how it is done. Example of abstraction in life:

When you go to withdraw money at an ATM, you do not care about how the ATM works or the components in the ATM, the only thing that you care about is the withdrawal feature. In this case, the unnecessary information of the ATM is hidden, which is abstraction.

Similarly in programming, when using an object you only need to care about the necessary methods and only need to know what that method is used for, regardless of source code and other methods. To implement abstraction in Java, you can use abstract class and interface.

Abstract class

An abstract class is a class that is declared with the abstract keyword and has the following characteristics:

  1. If a class is declared as an abstract class, you cannot initialize the object of that class, only the object of the subclass can be initialized. For example:
    //Declare abstract class Animal
    abstract class Animal{
    }
    
    class Cat extends Animal{
    }
    
    public class Entry {
    	public static void main(String[] args) {
    		Animal a = new Cat();
    	}
    }

    The above program will not return error but if you replace Animal a = new Cat(); into Animal a = new Animal(); then the program will return error (because you cannot initialize the object of the abstract class).

  2. An abstract class can have abstract methods. An abstract method is a method that has only the declaration part, no body. For example:
    // Declare abstract class Animal
    abstract class Animal {
    	// Declare abstract method makeSound()
    	public abstract void makeSound();
    }
  3. If a class is inherited from the abstract class, then that class must override all the abstract methods. For example:
    abstract class Animal {
    	public abstract void makeSound();
    }
    
    class Cat extends Animal {
    	@Override
    	public void makeSound() {
    		System.out.println("meow meow");
    	}
    }
    
    public class Entry {
    	public static void main(String[] args) {
    		Animal a = new Cat();
    		a.makeSound();
    	}
    }

    When the above code is compiled and executed, it produces the following result:

    meow meow


    In the above program, if the Cat class does not override the makeSound() method, the program will return an error like this:

An abstract class is often used to be the parent class for classes with the same nature. The abstract method will help you hide the implementation of the method. However, you can still declare body methods and attributes, so using the abstract class will not be considered completely abstract. To achieve complete abstraction you need to use the interface.

interface

interface is used to store abstract methods and constant variables. Some features of the interface:

  1. Like the abstract class, you can not initialize the object of the interface, you can only initialize the object of the class inherited from the interface.
  2. All the methods in the interface are understood by the compiler as abstract methods and all the variables in the interface are understood by the compiler as constants. For example, if you declare interface IAnimal like this:
    interface IAnimal{
    	int N = 4;
    	void move();
    	void sound();
    }

    The compile replies as below:

    interface IAnimal{
    	public static final int N = 4;
    	public abstract void move();
    	public abstract void sound();
    }
    
  3. A class can inherit multiple interfaces. As you know, Java is designed for simple purposes, so it does not support multiple inheritance with class, but because the interface only contains empty methods, Java allows a class to inherit multiple interface. For example:
    interface IFlyable {
    	void fly();
    }
    
    interface IEatable {
    	void eat();
    }
    
    class Bird implements IFlyable, IEatable {
    	@Override
    	public void fly() {
    		System.out.println("Bird flying");
    	}
    
    	@Override
    	public void eat() {
    		System.out.println("Bird eats");
    	}
    }
    
    public class Entry {
    	public static void main(String[] args) {
    		Bird bird = new Bird();
    		bird.eat();
    		bird.fly();
    	}
    }

    When the above code is compiled and executed, it produces the following result:

    Bird eats
    Bird flying
    

    Note: To inherit the interface, you use the implements keyword instead of extends.

When you look at the interface, the only thing you see is the abstract methods (features), so using the interface is considered as completely abstraction. You can create a separate interface to represent the features of a class and communicate with objects through the interface. For example, the Customer class will have an interface ICustomer, the Employee class will have an interface called IEmployee, ...

Instruction

Code sample:

File Person.java:

public abstract class Person {
	private String name;
	private String address;

	public Person(String name, String address) {
		this.name = name;
		this.address = address;
	}

	public abstract void display();

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
}

File Employee.java

public class Employee extends Person {
	private int salary;
	
	public Employee(String name, String address, int salary) {
		super(name, address);
		this.salary = salary;
	}

	@Override
	public void display() {
		System.out.println("Employee name: " + super.getName());
		System.out.println("Employee address: " + super.getAddress());
		System.out.println("Employee salary: " + salary);
	}	
}

File Customer.java

public class Customer extends Person {
	private int balance;

	public Customer(String name, String address, int balance) {
		super(name, address);
		this.balance = balance;
	}

	@Override
	public void display() {
		System.out.println("Customer name: " + super.getName());
		System.out.println("Customer address: " + super.getAddress());
		System.out.println("Customer balance: " + balance);
	}
}