I am little confused about abstraction in java.
I have checked many pages stating that abstraction is data hiding(Hiding the implementation).
What I understa
I'm not sure if this answers your question but if you're talking about abstract classes in general, they are there to provide functionality to a child class that extends it without the child class having to know or deal with all the details of the implementation (it's hidden from the user of the child class).
Let's take a car for example:
public abstract class Vehicle {
protected int _numberOfWheels;
public Vehicle() {
this._numberOfWheels = 4;
}
}
public class Truck extends Vehicle {
public int carryingLoad;
public Truck() {
this.carryingLoad = 4000; // kg or something
}
}
So vehicle is an abstract instance of a vehicle object and already has some functionality associated with it, such as the number of wheels. It's a protected method, so if I create a new instance of truck:
// Inside main
Truck truck = new Truck();
I cannot change the number of wheels, however, if I were to write a function for truck within the class like:
// Inside the Truck class
public void addSpareTire() {
this._numberOfWheels++;
}
So that I could call it like:
// Inside main
truck.addSpareTire();
I could still interact with some variables, but only thorough the functions in the class that extends the abstract class. I can add one tire at a time by calling addSpareTire()
, but I could never interact directly with _numberOfWheels
from the main function where I am using the Truck
object, but I can from inside the Truck
class declaration. From the user of the truck object, that information is hidden.
I don't know if this is what you're asking for. Hope this helps.