6 Replies - 461 Views - Last Post: 17 April 2011 - 07:18 AM Rate Topic: -----

#1 zake2  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 41
  • Joined: 24-February 11

Inheritance in java

Posted 17 April 2011 - 04:31 AM

I created three classes as sub classes of a certain class and each had certain methods to perform some operations. If I now want to introduce the main method, do I need to create another class that will contain this main or how do I deal with such a situation.
Look at the sketch code below


//parent class
public class me{

//fields to be shared by all the classes to be created
public int f;
public String hjjj;//etc

//creating 1st subclass
public class A extends me{

gdghfghhgf
jhfhjhjfjhf
}

// 2nd sub class
public class B extends me{

fddghdggdgd
dbddhdhdhdh
}

// 3rd subclass
public class C extends me{
hfhhfhhhghg
jkgjgjjgjgjgj
}

// How do I introduce the main assumng I have made some object instanciations

//Is it like eg
public UseDemo{

public static void main(String[] args){

}

}

}





Is This A Good Question/Topic? 0
  • +

Replies To: Inheritance in java

#2 Travis1012  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 220
  • Joined: 05-August 09

Re: Inheritance in java

Posted 17 April 2011 - 04:37 AM

You should not have your subclasses inside your superclass (class the subclasses inherit from). They should be separate classes.

As for the main method you can put it in another class, or put it in one of the subclasses, it all depends on how you want the structure of the program to be like.
Was This Post Helpful? 1
  • +
  • -

#3 zake2  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 41
  • Joined: 24-February 11

Re: Inheritance in java

Posted 17 April 2011 - 04:46 AM

View PostTravis1012, on 17 April 2011 - 04:37 AM, said:

You should not have your subclasses inside your superclass (class the subclasses inherit from). They should be separate classes.

As for the main method you can put it in another class, or put it in one of the subclasses, it all depends on how you want the structure of the program to be like.




Is there a way I can have all those classes in the same file for example "Housing.java" as the name of the file and the put all those subclasses in this and run as a single program?


Also, how when do I use "import java.io*;"

This post has been edited by zake2: 17 April 2011 - 04:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Inheritance in java

Posted 17 April 2011 - 04:52 AM

There can only be one public class per file, and since the main() method must be in a public class, it is sometimes in its own class and file. main() should be as small as possible, instantiating the other classes and passing the baton to them to do the work.

For example, in a classic MVC (Model-View-Controller) model, there will be at least 3 classes to handle the model, view, and controller duties. A third class is then used to contain the main() method. The main() method instantiates the model and view and then passes them to the controller, like:

package myprogram;

public class MyProgram
{
    public static void main( string[] args )
    {
        ProgramModel programModel = new ProgramModel();
        ProgramView programView = new ProgramView();
        new ProgramController( programModel, programView );
    }
}


and that's it. All of the real work is done by the other classes.

I'm not suggesting you use MVC or that it's the only/best way to structure a program. I was just using it as an example.

You can have multiple classes in a single file but only one of them can be public. I'm not sure why you want to or why you think it's a good idea to work that way, but it's possible.

You import java.io.* when you use classes in that "library". You can determine what you must import by referring to the API. For example, if you want to do file operations using Java's File class, you look at the java File class API on this page and see that to use the File class you must import java.io.File.

This post has been edited by GregBrannon: 17 April 2011 - 04:56 AM

Was This Post Helpful? 2
  • +
  • -

#5 zake2  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 41
  • Joined: 24-February 11

Re: Inheritance in java

Posted 17 April 2011 - 04:58 AM

View PostGregBrannon, on 17 April 2011 - 04:52 AM, said:

There can only be one public class per file, and since the main() method must be in a public class, it is sometimes in its own class and file. main() should be as small as possible, instantiating the other classes and passing the baton to them to do the work.

For example, in a classic MVC (Model-View-Controller) model, there will be at least 3 classes to handle the model, view, and controller duties. A third class is then used to contain the main() method. The main() method instantiates the model and view and then passes them to the controller, like:

package myprogram;

public class MyProgram
{
    public static void main( string[] args )
    {
        ProgramModel programModel = new ProgramModel();
        ProgramView programView = new ProgramView();
        new ProgramController( programModel, programView );
    }
}


and that's it. All of the real work is done by the other classes.

I'm not suggesting you use MVC or that it's the only/best way to structure a program. I was just using it as an example.

You can have multiple classes in a single file but only one of them can be public. I'm not sure why you want to or why you think it's a good idea to work that way, but it's possible.

You import java.io.* when you use classes in that "library". You can determine what you must import by referring to the API. For example, if you want to do file operations using Java's File class, you look at the java File class API on this page and see that to use the File class you must import java.io.File.




So I can't write a single program with all those classes and the main method?
Tell me how to use import java.io*;
Was This Post Helpful? 0
  • +
  • -

#6 Travis1012  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 220
  • Joined: 05-August 09

Re: Inheritance in java

Posted 17 April 2011 - 05:06 AM

You can write a single program with all those classes but the classes would need to be in separate files if you want them all to be public classes.

With import java.io.*; you put it at the top of each file that you need it to be imported into.
Was This Post Helpful? 2
  • +
  • -

#7 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: Inheritance in java

Posted 17 April 2011 - 07:18 AM

java.io is imported when we have to do input ans Stream related tasks.
It contains all the class you require to manage console as well as data storage related tasks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1