newbie interface question

H

happytoday

Dear All,
I am testing interface but really I can not apply what is explained in
oracle tutorials . Could you please explain why I have errors with
this code :

//*data of class Bicycle is cadence , gear , speed
//*methods of class Bicycle is changeCadence ,
IncreaseGearToIncreaseSpeed , IncreadSpeed , DeareaseSpeed
//*data of class Bicycle is Cadence , Gear , Speed

class prog01_BicycleDemo_inheirtance_trial01
{
public static void main (String [] argc )
{
Bicycle bike1 = new Bicycle();
MountainBicycle bike2 = new MountainBicycle();

bike1.speed=1;
bike1.cadence=1;
bike1.gear=1;
bike1.printresults();


bike1.speed=1;
bike1.cadence=1;
bike1.gear=1;
bike1.printresults();

bike1.speed=1;
bike1.cadence=1;
bike1.gear=1;
bike1.printresults();

bike2.speed=1;
bike2.cadence=1;
bike2.gear=1;
bike2.printresults();

bike2.mb_speed=1;
bike2.mb_cadence=1;
bike2.mb_gear=1;
bike2.mb_printresults();
}
}

class Bicycle
{
int speed;
int cadence;
int gear ;

void speed(int newspeed)
{
speed=newspeed;
}

void cadence(int newcadence)
{
cadence=newcadence;
}

void gear(int newgear)
{
gear=newgear;
}

void printresults()
{
System.out.println( "Cadence : " + cadence + " Gear : " + gear + "
Speed :" + speed );
}
}

class MountainBicycle extends Bicycle
{
int mb_speed;
int mb_cadence;
int mb_gear ;

void mb_speed(int newmb_speed)
{
mb_speed=newmb_speed;
}

void mb_cadence(int newmb_cadence)
{
mb_cadence=newmb_cadence;
}

void mb_gear(int newmb_gear)
{
mb_gear=newmb_gear;
}

void mb_printresults()
{
System.out.println( "MB_Cadence : " + mb_cadence + " MB_Gear : " +
mb_gear + " MB_Speed :" + mb_speed );
}
}
 
E

Eric Sosman

Dear All,
I am testing interface but really I can not apply what is explained in
oracle tutorials . Could you please explain why I have errors with
this code :
[...]

What "errors" do you have? For me, the code compiles just
fine, and runs producing the output

Cadence : 1 Gear : 1Speed:1
Cadence : 1 Gear : 1Speed:1
Cadence : 1 Gear : 1Speed:1
Cadence : 1 Gear : 1Speed:1
MB_Cadence : 1 MB_Gear : 1 MB_Speed :1

(Follow-ups set to c.l.j.help only, dropping c.l.j.programmer.)
 
J

Jens Vonderheide

happytoday said:
I am testing interface but really I can not apply what is explained in
oracle tutorials . Could you please explain why I have errors with
this code :

Besides the fact that your example may or may not compile, there are
some sever issues with the way to design and use classes.

For better encapsulation, do not directly access fields of an object
outside of it. You already wrote setters, but you are not using them.
Also Java best practice is to prefix getters with "get" and setters with
"set". If you look again into the Java Tutorial, you will see that the
Bicycle class is actually written that way.

So Bicycle and its usage would be

class Bicycle {
private int speed;
private int cadence;
private int gear;

public int getSpeed() { return speed; }
public void setSpeed(int speed) { this.speed = speed; }
// add other getters and setters in the same way
public void printResults() {
System.out.println("Bike: [" + speed + ", " + cadence +
", " + gear + "]");
}
}

Bicycle bike1 = new Bicycle();
bike1.setSpeed(1);
// set other data
bike1.printResults();

This way, you can later add checks to the setters (i.e. if your bike
only has 10 gears, you can handle setting it you gear 11 in an
"intelligent" way).

Also, there is no need to redeclare fields of a class that you inherit
from. Since speed, cadence and gear are already declared for Bicycle,
you do not need to redeclare them for MountainBicycle:

class MountainBicycle extends Bicycle {
public void printResults() {
System.out.println("MountainBike: [" + speed + ", " + cadence +
", " + gear + "]");
}
}
Bicycle bike2 = new MountainBicycle();
bike2.setSpeed(1);
// set other data
bike2.printResults();


BTW: Another good practice is instead of your printResults() method to
declare a method called toString() that converts the data to a String
and then use System.out.println from the caller:
class Bicycle() {
// ...
public String toString() {
return "Bike: [" + speed + ", " + cadence +
", " + gear + "]";
}
}
Bicycle bike1 = new Bicycle();
System.out.println(bike1.toString());

This allows more flexibility. For example, if you decide to write the
data to stderr instead of stdout, you only need to change the calling
code to
System.err.println(bike1.toString());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top