-1

So I have a project where I need to randomnly generate numbers to a specified number in Java. I have that part working and it takes user input, writes to a file and reads it. But here is my problem. The last portion of the assignment specifies that it must be in multiple classes. For example my teacher had a similar project where he could incorporate the code from a seperate .java file into the main one. It looked like this ReadFileExample readur = new ReadFileExample();

I was just wondering is that what class inheritance is and how do I incorporate it into my project. I have read the JavaDocs and I am still a little confused

2 Answers2

1

If I am understanding you correctly - I think you are confused about inheritance and just separate classes.

Whilst it is possible to have multiple class definitions in the same file (and in some cases , such as when using inner classes this is good / required) in general it is best practice to have separate class files. You can then use those classes from other classes.

In the example you gave ReadFileExample readur = new ReadFileExample(); what that is doing is:

  1. Creating a reference variable of type ReadFileExample called readur
  2. Creating a ReadFileExample object (the new ReadFileExample part)
  3. Assigning that object to the reference variable

You can then use the reference variable to access the instance variables and methods of the object.

Inheritance is different - with inheritance you create a parent/child type relationship between classes with the parent class being called the Superclass and the child class subclass. The subclass then inherits values from the Superclass such as its instance variables and methods (what exactly depends on what modifiers have been applied such as private or public)

An example :

You have a class public class Animal which is your superclass and contains instance variable size and color plus a method sleep(). You then create a subclass called Dog and you make it inherit the Animal class by using the extends keyword public class Dog extends Animal - this class now automatically has instance variables size and color plus a sleep() method .

There is obviously a lot more to it but that is the basics

A good book that explains all these concepts in a easy to understand way is Head First Java - if you want to learn about java OOP that is a good place to start.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
boliviab
  • 83
  • 2
  • 14
  • That's the perfect answer to my question. Thanks sir. So when I create an object reference how would I call it? For example would I use something like call readur? – Justin Safa Apr 12 '15 at 18:53
  • hi yes , in the example you gave you would use the variable name readur to access the object followed by a fullstop / dot then the member you are trying to use/access (members are the instance variables and methods of the class/object) . I.e. readur.methodName() or readur.instanceVariableName – boliviab Apr 12 '15 at 19:19
0

Each .java files represents a different class, what your teacher means seems to be design your code in separating concerns. Here I can see 'takes user input', 'writes to a files' and 'read it' as different concerns and each can be in his own file/class