inheritance setter and getter in java

Solutions on MaxInterview for inheritance setter and getter in java by the best coders in the world

showing results for - "inheritance setter and getter in java"
Edoardo
05 Aug 2017
1public class C3 extends C2{
2    protected int x = 3;
3}
Noemi
03 Jun 2017
1package testvehicle;
2
3
4public class Car extends Vehicle
5{
6
7    private int numDoors;
8    private int numWheels;
9
10
11    public Car(String manufacturer,String model,int maxSpeed,double price,int numWheels
12            ,int numDoors)
13    {
14        super(manufacturer,model,maxSpeed,price);
15        this.numDoors=numDoors;
16        this.numWheels=numWheels;
17
18    }
19
20    public Car()
21    {
22
23    }
24
25
26    public int getNumDoors()
27    {
28        return numDoors;
29    }
30
31
32    public void setNumDoors(int numDoors)
33    {
34        this.numDoors = numDoors;
35    }
36
37
38    public int getNumWheels()
39    {
40        return numWheels;
41    }
42
43
44    public void setNumWheels(int numWheels)
45    {
46        this.numWheels = numWheels;
47    }
48
49    public String toString()
50    {
51        return ("Number of doors:"+numDoors+"\n"+"Number of wheels:"+numWheels+""
52                + "\n"+
53        "Manufacturer:"+manufacturer+"\n"+
54               "Model:"+model+"\n"+"Maximum Speed:"+maxSpeed+"\n"+"Price in euros:"+price+
55               "\n");
56    }
57
58}
59
60
61
62package testvehicle;
63
64
65public class MotorCycle extends Vehicle
66{
67    private String seat;
68
69    public MotorCycle(String manufacturer,String model,int maxSpeed,double price
70            ,String seat)
71    {
72        super( manufacturer, model, maxSpeed, price);
73        this.seat=seat;
74    }
75
76
77    public MotorCycle()
78    {
79
80    }
81
82
83    public String getSeat()
84    {
85        return seat;
86    }
87
88
89    public void setSeat(String seat)
90    {
91        this.seat = seat;
92    }
93
94
95    public String toString()
96    {
97        return ("Manufacturer:"+manufacturer+"\n"+
98               "Model:"+model+"\n"+"Maximum Speed:"+maxSpeed+"\n"+"Price in euros:"+price+
99               "\n"+"Seat type:"+seat+"\n");
100    }
101
102
103
104
105}
106
107
108
109 package testvehicle;
110
111    public abstract class Vehicle//This class doesn't do something!
112    {
113        protected String manufacturer;
114        protected String model;
115        protected int maxSpeed;
116        protected double price;
117
118        public Vehicle(String manufacturer,String model,int maxSpeed,double price)
119        {
120            this.manufacturer=manufacturer;
121            this.model=model;
122            this.maxSpeed=maxSpeed;
123            this.price=price;
124
125        }
126
127        public Vehicle()
128        {
129
130        }
131
132
133        public String getManufacturer()
134        {
135            return manufacturer;
136        }
137
138
139        public void setManufacturer(String manufacturer)
140        {
141            this.manufacturer = manufacturer;
142        }
143
144
145        public String getModel()
146        {
147            return model;
148        }
149
150
151        public void setModel(String model)
152        {
153            this.model = model;
154        }
155
156
157        public int getMaxSpeed()
158        {
159            return maxSpeed;
160        }
161
162
163        public void setMaxSpeed(int maxSpeed)
164        {
165            this.maxSpeed = maxSpeed;
166        }
167
168
169        public double getPrice()
170        {
171            return price;
172        }
173
174
175        public void setPrice(double price)
176        {
177            this.price = price;
178        }
179
180
181
182       public String toString()
183        {
184           return ("Manufacturer:"+manufacturer+"\n"+
185                   "Model:"+model+"\n"+"Maximum Speed:"+maxSpeed+"\n"+"Price in euros:"+price+
186                   "\n");
187        }
188
189
190
191    }
192
193    package testvehicle;
194
195    public class Main
196    {
197
198
199        public static void main(String[] args)
200        {
201            Car C=new Car("Opel","Corsa",220,12000.0,4,5);
202            MotorCycle M=new MotorCycle("KTM","DUKE-690",250,9000.0,"Agressive");
203            System.out.println(C.toString());
204            System.out.println();
205            System.out.println(M.toString());
206
207        }
208
209
210    }
Jonas
23 Aug 2018
1public class C1 {
2    protected int x = 1;
3
4    public int getX() {
5        return x;
6    }
7
8    public void setX(int x) {
9        this.x = x;
10    }
11
12    public static void main(String[] args){
13        System.out.println(new C1().getX());
14        System.out.println(new C2().getX());
15        System.out.println(new C3().getX());
16        System.out.println(new C4().getX());
17    }
18
19}
Moritz
04 Nov 2017
1public class C4 extends C3{
2
3    protected int x = 4;
4
5    @Override
6    public int getX() {
7        return x;
8    }
9
10}
Giorgio
26 Mar 2020
1public class A {
2    private int a;
3    public int getA() {
4        return a;
5    }
6    public void setA(int value) {
7        a = value;
8    }
9}
Sara
17 Jan 2017
1public class C2 extends C1{
2}
María
28 Apr 2016
1public class B extends A {
2    @Override
3    public final int getA() {
4        return super.getA();
5    }
6    @Override
7    public final void setA(int value) {
8        super.setA(value);
9    }
10}
Mickey
18 Sep 2016
1C1.x = 1
2C2.x = 1
3C3.x = 1
4C4.x = 4