• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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) {

}
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds as if you want to make Vehicle and its expense method abstract.
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?

 
kri shan
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The java tutorial here, will help with selection of class or interface. but you do need to correct the code.
 
Saifuddin Merchant
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Not unless Car is abstract.

I am not clear. Ca you explain with example ?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic