This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Car Factory  RSS feed

 
Adam Turner
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just made a car factory


Car


Honda



Machine Interface
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't said whet your question is.
In the interface (ans elsewhere) methods shou‍ld start with lowerCaseLetters.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have a subclass of Car, you shou‍ld not duplicate its public methods. Only write the methods again if you are intending to override them (and then always preface them with the predefined annotation @Override). Never try to duplicate the fields in a subclass. What happens in the Honda class is that you have different values of distance (sorry, mistake) mileage from the Car class. Duplicating fields is a sure‑fire recipe for severe confusion.
Also, I think you shou‍ld give those classes constructors, and initialise every field in the constructor. Remember you will need super(...); in the Honda constructor.
That is a strange definition of rpm, but the value you get will not be what you expect. You are using a different formula in the get method, but your getRPM method shou‍ld simply return the field, rather than trying to calculate it.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Please read this for Naming Conventions
  • You can override toString() method to display information.
  • As all cars will have common method like start() and stop() and any other attributes of Machine so I think only Car class need to implement Machine interface as It can be inherited by Hond and all sub classes of Car.
  • Machine interface can have engineNo field as each engine has unique number which will be final.
  •  
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You don't have to create an object of Car directly rather create an object of Honda through which you can access to the properties of Car.
     
    Campbell Ritchie
    Marshal
    Posts: 55717
    163
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganesh Patekar wrote:. . .
  • Machine interface can have engineNo field as each engine has unique number which will be final.
  • I thought interface fields were used as constants, That isn't what a serial number is, which is more of a final instance field, different for all instances.
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Ganesh Patekar wrote:. . .
  • Machine interface can have engineNo field as each engine has unique number which will be final.
  • I thought interface fields were used as constants, That isn't what a serial number is, which is more of a final instance field, different for all instances.
    Oh yes then It will be same for all instances and we don't want all machines to have same engine no.
     
    Adam Turner
    Greenhorn
    Posts: 22
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No question really, your input is helpful enough. I'm just familiarising myself with Java in advance of a masters degree where it will be taught from scratch.
     
    Adam Turner
    Greenhorn
    Posts: 22
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganesh Patekar wrote:
  • Please read this for Naming Conventions
  • You can override toString() method to display information.
  • As all cars will have common method like start() and stop() and any other attributes of Machine so I think only Car class need to implement Machine interface as It can be inherited by Hond and all sub classes of Car.
  • Machine interface can have engineNo field as each engine has unique number which will be final.


  • - Can correct naming convention, fine!
    - Have amended other classes to not implement machine interface.
    - How do I override toString method? Can you explain in a little more detail what this does and why it is a benefit over how I've written this?
     
    Campbell Ritchie
    Marshal
    Posts: 55717
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It makes little difference whether you write implements Machine in the subclasses. If the superclass implements interface X then all its subclasses implement interface X. Maybe the simplest way to find that out is to run the javadoc tool on all your .java files. It will work a lot better if you write some documentation comments, but that might be too much work for you to do in the next five minutes. At the top of the page for each class, it will show an inheritance hierarchy with Object at the top, and a list of interface implemented. If you run
    javadoc *.java
    you can get documentation pages for all the classes in your package.

    Overriding the toString method? Easy. Start by reading about it in the Object class, and have a look at the Java™ Tutorials. Then you decide what you are going to return. It will be a String and you can create that String all sorts of ways. The String#format() method is one way to do it. If you know about printf, format works very similarly.Beware of being too specific in the documentation comment, otherwise you might commit yourself to a particular type of String to return. There is a section in Effective Java by Joshua Bloch about toString, in chapter 3.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!