Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8295031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:26:26+00:00 2026-06-08T14:26:26+00:00

Say I have two classes class Driver{ //attributes of driver ,ex: driving licence number

  • 0

Say I have two classes

class Driver{
    //attributes of driver ,ex: driving licence number
    // methods related to driving ,ex: drive(Car) , stop(Car)
    changeTyre(Car,Tyre);   // sometimes the driver can change the tyres right?
}
class Mechanic{
    // Hard mechanical stuff , ex: repairEngine(Car)
    changeTyre(Car,Tyre);  // Simple.hence sometimes the driver also does
}

Now the implementations of the two changeTyre() methods will be the same.

Now I have two issues,

  1. There is a repetition (duplication) of code
  2. It doesn’t seem meaningful to have a Super class containing the changeTyre(Car,Tyre) method

How these kind of situations handled?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:26:28+00:00Added an answer on June 8, 2026 at 2:26 pm

    To expand on using composition over inheritance (Willie’s answer), I think you were on the right track in your comment about using ChangeTyre like car.ChangeTyre(Tyre).

    Each mechanic or driver will be associated to a car, so they can have a Car property –

    class Driver{
    
        Car driverCar;
    
        //constructor
        Driver(Car car)
        {
            driverCar = car;
        }
    
        //attributes of driver ,ex: driving licence number
        // methods related to driving ,ex: drive(Car) , stop(Car)
        changeTyre(Tyre) 
        {
            driverCar.changeTyre(Tyre);
        }
    }
    

    They changeTyre method of Driver and Mechanic might be the same, but the actual logic to change the tyre will live in one place. I don’t think inheritance works because a mechanic is not a driver, and inheritance supports an “is-a” relationship. It may not make sense to force changeTyre to a superclass (say Person), because not every class that inherits from Person would need to have a changeTyre method. See this question for more information about composition over inheritance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For simplicity let's say I have two classes: a class called Car and a
Let's say we have these two classes: public class Base { public static int
Say you have two classes, order and customer: public class Customer{ public int CustomerId
Say I have two classes, and neither of them are GUI components. Class A
Say I have two separate classes, A and B. I also have Repository class
Newbie question. I have two c# classes - a code class (say, CodeClass) and
Let's say I have a base class 'A' and two classes 'B' and 'C',
Say i have the following two classes: public class User { public int ID
Let's say I have two classes. Each class has one parameter. Parameter of first
Lets say I have two classes: class A { [SortOrder(3)] public string Name {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.