File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes method Overriding - Class Bus extends Vehicle,  Class Car extends Vehicle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "method Overriding - Class Bus extends Vehicle,  Class Car extends Vehicle" Watch "method Overriding - Class Bus extends Vehicle,  Class Car extends Vehicle" New topic
Author

method Overriding - Class Bus extends Vehicle, Class Car extends Vehicle

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1371
Class Car {

public int expense(int mileage) {

}
}


I am planning to separate Class Car as Sub class instead of individiual class for genealization. I created class Vehicle as Super class and moved some of the genealized methods to Class Vehicle and created new sub class Bus. Class Bus extends from Class Vehicle too.
Class Car has expense() method and takes mileage as arguments. I created expense method in Vehicle.

Class Vehicle {

public int expense(int mileage) {

}
}

Class Bus extends Vehicle {

public int expense(int mileage) {

int expense = milege * 2;
}

}



Class Car extends Vehicle {

public int expense(int mileage) {

int expense = milege * 5;
}

}


Can i just declare expense method without implementation in Class Vehicle like
public int expense(int mileage) {

}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Sounds as if you want to make Vehicle and its expense method abstract.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

kri shan wrote:Can i just declare expense method without implementation in Class Vehicle like
public int expense(int mileage) {}.


What happens when you tried it?


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1371
My sample code:

Abstract Class Vehicle extends Thread{

public abstract int expense(int mileage) {

}

public void run() {

}
}

Class Bus extends Vehicle {

public int expense(int mileage) {

int expense = milege * 2;
}

}


or can i make Vehicle interface ?
Larry Frissell
Ranch Hand

Joined: May 16, 2008
Posts: 82
    
    2
The java tutorial here, will help with selection of class or interface. but you do need to correct the code.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

Vehicle should not extends Thread class - since it is definitely not 'a thread'.

If threaded behavior is needed in Vehicle it would be better to implement the Runnable interface.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1371
Abstract Class Vehicle extends Thread{

public abstract int expense(int mileage);


}


Class Bus extends Vehicle {

public int expense(int mileage) {

int expense = milege * 2;
}

public void run() {
// code
}


}


Class Car extends Vehicle {
public void run() {
// code
}
}

Can i implement abstract method only in the Class Bus and not in the Class Car.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
kri shan wrote:Can i implement abstract method only in the Class Bus and not in the Class Car.
Not unless Car is abstract.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1371
Not unless Car is abstract.
I am not clear. Ca you explain with example ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
kri shan wrote:Abstract Class Vehicle extends Thread{

public abstract int expense(int mileage);


}
There is an example of an abstract class. If you want expense to be abstract in a class, that class has to be declared abstract. Like Vehicle.
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1371
Not unless Car is abstract

'
I am explaining more elaborate.

My super class Vehicle extends Thread. Hence any subclass extends from Vehicle may implement run method. Super class Vehcicle is abstract. Hence whichever sub class is going to extend from Vehcile Super class should implement expense method.

I want to implement expense method only in sub class 'Bus', not in the sub class 'Car'. Why should i implement sub Class Car as Abstract ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

kri shan wrote:
I want to implement expense method only in sub class 'Bus', not in the sub class 'Car'. Why should i implement sub Class Car as Abstract ?


You don't have a choice. Concrete classes are not allowed to have unimplemented methods. So, if you car class doesn't implement the expense() method, required by the Vehicle class, it will have to allow be abstract.

Henry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Because a Car is a Vehicle according to your inheritance tree. If a Vehicle has an "expenses" method then all its subclasses have an "expenses" method. If you want Car without an "expenses" method, then it is NOT-A Vehicle.
Vehicle-expenses = Car-expenses. If you want a Car object then it has to have an expenses method working.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: method Overriding - Class Bus extends Vehicle, Class Car extends Vehicle
 
Similar Threads
mock test for generics & collections
DownCast and Generics
How is this Possible?
Abstract class - Whetehere sub class should implement all the abstract methods of base class ?
Overriding/overloading