inheritance setter and getter in java

package testvehicle;


public class Car extends Vehicle
{

    private int numDoors;
    private int numWheels;


    public Car(String manufacturer,String model,int maxSpeed,double price,int numWheels
            ,int numDoors)
    {
        super(manufacturer,model,maxSpeed,price);
        this.numDoors=numDoors;
        this.numWheels=numWheels;

    }

    public Car()
    {

    }


    public int getNumDoors()
    {
        return numDoors;
    }


    public void setNumDoors(int numDoors)
    {
        this.numDoors = numDoors;
    }


    public int getNumWheels()
    {
        return numWheels;
    }


    public void setNumWheels(int numWheels)
    {
        this.numWheels = numWheels;
    }

    public String toString()
    {
        return ("Number of doors:"+numDoors+"\n"+"Number of wheels:"+numWheels+""
                + "\n"+
        "Manufacturer:"+manufacturer+"\n"+
               "Model:"+model+"\n"+"Maximum Speed:"+maxSpeed+"\n"+"Price in euros:"+price+
               "\n");
    }

}



package testvehicle;


public class MotorCycle extends Vehicle
{
    private String seat;

    public MotorCycle(String manufacturer,String model,int maxSpeed,double price
            ,String seat)
    {
        super( manufacturer, model, maxSpeed, price);
        this.seat=seat;
    }


    public MotorCycle()
    {

    }


    public String getSeat()
    {
        return seat;
    }


    public void setSeat(String seat)
    {
        this.seat = seat;
    }


    public String toString()
    {
        return ("Manufacturer:"+manufacturer+"\n"+
               "Model:"+model+"\n"+"Maximum Speed:"+maxSpeed+"\n"+"Price in euros:"+price+
               "\n"+"Seat type:"+seat+"\n");
    }




}



 package testvehicle;

    public abstract class Vehicle//This class doesn't do something!
    {
        protected String manufacturer;
        protected String model;
        protected int maxSpeed;
        protected double price;

        public Vehicle(String manufacturer,String model,int maxSpeed,double price)
        {
            this.manufacturer=manufacturer;
            this.model=model;
            this.maxSpeed=maxSpeed;
            this.price=price;

        }

        public Vehicle()
        {

        }


        public String getManufacturer()
        {
            return manufacturer;
        }


        public void setManufacturer(String manufacturer)
        {
            this.manufacturer = manufacturer;
        }


        public String getModel()
        {
            return model;
        }


        public void setModel(String model)
        {
            this.model = model;
        }


        public int getMaxSpeed()
        {
            return maxSpeed;
        }


        public void setMaxSpeed(int maxSpeed)
        {
            this.maxSpeed = maxSpeed;
        }


        public double getPrice()
        {
            return price;
        }


        public void setPrice(double price)
        {
            this.price = price;
        }



       public String toString()
        {
           return ("Manufacturer:"+manufacturer+"\n"+
                   "Model:"+model+"\n"+"Maximum Speed:"+maxSpeed+"\n"+"Price in euros:"+price+
                   "\n");
        }



    }

    package testvehicle;

    public class Main
    {


        public static void main(String[] args)
        {
            Car C=new Car("Opel","Corsa",220,12000.0,4,5);
            MotorCycle M=new MotorCycle("KTM","DUKE-690",250,9000.0,"Agressive");
            System.out.println(C.toString());
            System.out.println();
            System.out.println(M.toString());

        }


    }

4.25
8
Lionel Aguero 33605 points

                                    public class C2 extends C1{
}

4.25 (8 Votes)
0
4.13
8
Krish 100200 points

                                    public class C4 extends C3{

    protected int x = 4;

    @Override
    public int getX() {
        return x;
    }

}

4.13 (8 Votes)
0
4.25
8
Awgiedawgie 440220 points

                                    C1.x = 1
C2.x = 1
C3.x = 1
C4.x = 4

4.25 (8 Votes)
0
3.8
10
Awgiedawgie 440220 points

                                    public class A {
    private int a;
    public int getA() {
        return a;
    }
    public void setA(int value) {
        a = value;
    }
}

3.8 (10 Votes)
0
3.6
5
IllusiveBrian 18110 points

                                    public class C1 {
    protected int x = 1;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public static void main(String[] args){
        System.out.println(new C1().getX());
        System.out.println(new C2().getX());
        System.out.println(new C3().getX());
        System.out.println(new C4().getX());
    }

}

3.6 (5 Votes)
0
0
0
Awgiedawgie 440220 points

                                    public class B extends A {
    @Override
    public final int getA() {
        return super.getA();
    }
    @Override
    public final void setA(int value) {
        super.setA(value);
    }
}

0
0
3.86
7
Phoenix Logan 186120 points

                                    public class C3 extends C2{
    protected int x = 3;
}

3.86 (7 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source