- 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
andaddress
areprivate
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 classPerson
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, ifname = "Trung", address = "HN", salary = 3300
. when we call thedisplay()
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 classPerson
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, ifname = "Linh", address = "BN", balance = 10400
, when we calldisplay()
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:
- 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();
intoAnimal a = new Animal();
then the program will return error (because you cannot initialize the object of the abstract class). - 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(); }
- 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 theCat
class does not override themakeSound()
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:
- 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.
- 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(); }
- 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 theinterface
only contains empty methods, Java allows a class to inherit multipleinterface
. 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 theimplements
keyword instead ofextends
.
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);
}
}
- Lớp Person là lớp trừu tượng.
- Lớp Person có thuộc tính name với phạm vi truy cập private
- Lớp Person có thuộc tính address với phạm vi truy cập private
- Lớp Employee và Customer được kế thừa từ lớp Person.
- Lớp Employee có phương thức khởi tạo Employee(name: String, address: String, salary: int) và các getter được cài đặt đúng theo yêu cầu đề bài.
- Lớp Employee có phương thức display được cài đặt đúng theo yêu cầu đề bài.
- Lớp Customer có phương thức display được cài đặt giống như yêu cầu đề bài
- Lớp Customer có phương thức khởi tạo Customer(name; String, address: String, balance: int) và các getter được cài đặt đúng theo yêu cầu đề bài.