본문 바로가기

Computer Science/Concept

Features of Object Oriented Programming - Abstraction

Absctraction is one of the most strongest and powerful feature of Object Oriented Programming.

It can bind common properties together and manage them in same category.

 

For example, when there is a Lion, Tiger, Leopard, Whale, Fish, Seatutle. We can categorize by them as animal. 

However, we can categorize by them as a mammal and non mammal, Cat family and non cat family or land aniaml or sea animal.

 

Overall abstaraction is generalizing properties by combine common parts and discard different parts of objects, or simplify an object by indiciating important parts and ignore less important parts.

 

We can apply this to a programming. 

 

Example

 

Let say we have a 2 different cars, trucks and convertable. They both have different Max speed, Tire size, and different moves. However, even they have different properties, we can combine them as tireSize, maxSpeed and moves. 

 

We can manage the common parts with absctraction.

 

 

package abstraction;

public abstract class Car {
	
	private int tireSize;
	private int maxSpeed;
	private String nameOfCar;
	
	public Car(int tireSize, int maxSpeed, String nameOfCar) {
		
		this.tireSize = tireSize;
		this.maxSpeed = maxSpeed;
		this.nameOfCar = nameOfCar;
	}
	
	abstract void move();
	
	public void printTireSizeAndMaxSpeed() {
		System.out.println("Tire Size of " + nameOfCar + " is " + tireSize + " and Max Speed is : " + maxSpeed + "mph");
	}


}

 

However, since they have different tiresize, maxspeed, name and moves we can make them implement in their own class by useing super, and Override.

 

super:  Allows a child class (which is Truck and Convertable this case) to implement or more like send their own properties to Parent (which is Car in this case)

 

Override: Allows a child class to implement their unique function

 

package abstraction;

public class Truck extends Car{

	public Truck(int tireSize, int maxSpeed, String nameOfCar) {
		super(tireSize, maxSpeed, nameOfCar);
		// TODO Auto-generated constructor stub
	}

	@Override
	void move() {
		System.out.println("I am a Truck, I can move very heavy stuffs!");
		
	}

}
package abstraction;

public class Convertable extends Car{

	public Convertable(int tireSize, int maxSpeed, String nameOfCar) {
		super(tireSize, maxSpeed, nameOfCar);
		// TODO Auto-generated constructor stub
	}

	@Override
	void move() {
		System.out.println("I am convertable, I can remove a top!");
	}

}

 

As we can see myTruck and myConvertable is both car and we create an instance (by using new keyword) as their own and we pass in different Values.

 

package abstraction;

public class CarMain {
	public static void main(String[] args) {
		Car myTruck = new Truck(10, 120, "Raptor");
		Car myConvertable = new Convertable(9, 190, "Miyata");
		
		myTruck.move();
		myTruck.printTireSizeAndMaxSpeed();
		
		myConvertable.move();
		myConvertable.printTireSizeAndMaxSpeed();
		
	}

}

 

And here is the Resullt. 

 

 

OverAll

With the example we use inheritance to visualize how to use abstraction but there are many different ways. The key point of Abstraction is, removing unimporatnant or uncommon properties and categorize or grouping important or common properties.  

'Computer Science > Concept' 카테고리의 다른 글

Object Oriented Programming  (0) 2023.05.02