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:
- Creating a reference variable of type
ReadFileExample
called readur
- Creating a
ReadFileExample
object (the new ReadFileExample
part)
- 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.