3

I am trying to understand better oop ,but I don't understand how to use abstract classes. I have a car class, and a car can be mercedes , audi and volvo. Mercedes cars can have 5 passengers ,can open door and block windows; audi car can have 6 passengers, and just can open door. Volvo car can only have 3 passengers. So, in order to do this, I created an interface:

public interface Car{
void openDoor();
void blockWindows();
int passengers=0;
}

then for each car I created an abstract class:

public abstract class  Mercedes implements Car{
public void openDoor(){
    System.out.println("Mercedes opendoor");
}
  public void blockWindow(){
    System.out.println("Mercedes blockwindow");
}
public Mercedes()
{
int passengers=5;
}



 public abstract class  Audi implements Car{
public void openDoor(){
    System.out.println("Audi opendoor");
}
public Audi()
{
int passengers=6;
}
  }

public abstract class  Volvo implements Car{

public Volvo()
{
int passengers=6;
}

Now, I need to create an object that can transport maximum 15 cars. So I wrote:

public class TransportCars{
Car[] transport=new Car[15];}

//now I need to put in transport array differents types of cars. But I can not instantiate abstract classes. Should I use anything else? I used abstract classes because I can implement an interface and use just o part of it

6
  • 4
    Why abstract? Why not just normal classes?! Commented Nov 18, 2013 at 9:27
  • 2
    I think what you really want is to make Car the abstract class and have the different types of cars be subclasses. Commented Nov 18, 2013 at 9:28
  • 2
    why you using abstract classes? Commented Nov 18, 2013 at 9:28
  • @R.J I used abstract classes because using normal classes, I am forced to implement all the methods from interface. So, I was thinking is better to use abstract classes Commented Nov 18, 2013 at 9:30
  • 1
    You won't be able to instantiate the abstract classes. That defeats the total purpose of this. Commented Nov 18, 2013 at 9:32

5 Answers 5

3

Basically your design is completely wrong, as you are yet new to java, you first need to understand basic.

Design should be like this :

Car is a Vehical, so is-a relationship.

So you can create a Class Vehicle.

class Vehicle {
   // properties of Vehicle like type of Vehicle, numberOfWheels etc.
   String vType;
   int numberOfWheels;
   int passengers;
}

// Car is a Vehicle so it should extend Vehicle
class Car extends Vehicle {
    String type; // sedan or hatchback
    String manufacturer; // Mercedes, BMW, Audi, Volvo etc.
}

If you want to restrict Vehicle not to be instantiated, you can declare it as an abstract class

Sign up to request clarification or add additional context in comments.

Comments

3

Interfaces and abstract classes have some similarities, but are not the same. For your example, you should probably make Car an abstract and have Mercedes, Audi, and Volvo extend that abstract class. Then make sure to implement any abstract methods in Car in each of your classes which extend it. Doing so will make them concrete classes, which can be instantiated.

One thing you can do with abstract classes which you cannot with interfaces, is include data. I see you're already doing that with your interface for number of passengers, but in an interface, the value will be static and final.

2 Comments

So, if I don't want volvo car to have this services: opendoor and blockwindows, I just should write in my normal class : void blockWindow(){}; ?
No. Also, not sure what you mean by normal class. Basically, you should put things which are common to all cars in the Car class. Things which are specific to Volvo go in the Volvo class. So, in this case, they're all pretty much the same. But if you had a Batmobile class which extends Car, then fireRockets() would go in the Batmobile class.
2

I see the problem why it's hard for you to understant abstraction, it's because your example is wrong. The type of car must be a concrete class which inherits from an abstract class. The specificity of an abstract class is that you can't create one of it, you can only inherit it ,that benefits polymorphism. But the real benefits comes from abstract methods.

Instead of creating a Car interface ,create a Vehicle interface. Since you don't know how many passengers each type of car can carry make Car an abstract class. Every vehicle have to start and stop. And you know that a car must load the passengers first in order to start. In the end you can start all your vehicles regardless what type car is it , οr what type of vehicle.

interface Vehicle {
    public start();
    public stop();
}

abstract class Car implements Vehicle {
    protected wheels = 4;

    public start() {
        loadPassengers();
        // do extra stuff like 
        //closeDoors();
    }

    abstract public loadPassengers();
}

public class Volvo extends Car {
    int passengers  =  6;

    public loadPassengers() {
         doSomething(this.passengers);
    }
}

public static void main() {
    List<Car> cars = new ArrayList<Car>();
    cars.add(new Volvo());
    cars.add(new Mercedes());

    for(Car car : cars) {
        car.start();
    }
}

Comments

2

In terms of relationships between classes, there's notmuch differences between abstract classes and interfaces: You can't instantiate any of them and they would be used as a template for objects depending on them. You can implement partially the methods of an abstract class however, but even if all of them are implemented still you can't instantiate a class defined as abstract. To be concise:

Interfaces:

  • Define methods.
  • A class can implement several interfaces.
  • Public visibility (or package, by default).
  • Can't be instantiated.

Abstract classes:

  • Define methods and may implement them.
  • A class can inherit from only one class (abstract or not).
  • User defined visibility.
  • Can't be instantiated.

If your cars are meant to implement several interfaces, use interfaces, but their scope will have to be public or package. If you just want to have an inheritance relation with one class, use abstract.

Comments

2

It seems that you are going about this the wrong way. It would be better for you to seek a tutorial, but I will try to clear up as much as I can for you:

The car class should be either an interface or an abstract class (or neither). If it's an interface, than Mercedes, Audi and Volvo should implement it. If that is the case, any method in "car" must be implemented in the others, so all of them must have "Open door" and "Block windows". you must choose for each of the implementing classes how it will implement it.

If it's an abstract class, you can have some of the methods implemented in "car" and they will work "as is" in Mercedes, Audi and Volvo (which will "extend" car), unless you re-define them in their respective classes. if you want to enforce their implementation in each of inheriting classes, you can define those methods to be abstract in "car", and not implement them in car at all.

If you want to implement all of the methods in car, you don't need it to be abstract at all. You could still re-define them as mentioned above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.