Loading

How to Use OOP (Object Oriented Programming) In Java

Submitted by: 
Language: 
Visitors have accessed this post 3512 times.


Introduction:
Welcome to my tutorial on how to use OOP (Object Oriented Programming), I am using Java to do this however, the principle applies for OOP in general.

Steps of Creation:
Step 1:

First I have created a simple project in Java IDE called Eclipse. I have two classes, one called Main to host the form/frame/window of the application and handle the output, and a Game class which is going to be our OOP class.

Basic Main.java:

  1. import javax.swing.JFrame;
  2. public class Main extends JFrame{
  3. JFrame frame = new JFrame();
  4. public Main(){
  5. Dimension dim = new Dimension(720, 480);
  6. frame.setTitle("OOP Tutorial");
  7. frame.setSize(dim);
  8. frame.setPreferredSize(dim);
  9. frame.setMinimumSize(dim);
  10. frame.setMaximumSize(dim);
  11. frame.setLocationRelativeTo(null);
  12. frame.pack();
  13. frame.setVisible(true);
  14. begin();
  15. }
  16.  
  17. public static void main(String args[]){
  18. new Main();
  19. }
  20. }

This is simple Java and just simply creates a custom JFrame and runs it on running the program.

Step 2:
Now that we have our basic frame we can start our OOP. In our Game.java class we are going to create two Strings, one is title and the other is platform. I am using this OOP tutorial in the form of a games list, you can use whatever you want. We also want to add a constructor in to our Game.java class which is going to take two parameters, one for the name and another for the platform then set our global variables to the appropriate parameter.

  1. public class Game {
  2. public String title = "";
  3. public String platform = "";
  4. public Game(String name, String plat){
  5. this.title = name;
  6. this.platform = plat;
  7. }
  8. public void setPlatform(String plat){
  9. this.platform = plat;
  10. }
  11. }

I have also created a simple void to change the platform of our 'game' later on.

Step 3:
Now we can begin to use our OOP class. Here is the basics of how OOP works:

Normally, a classes variables will be kept the same for ever instance of the class that is running, therefore if variable 'a' gets set to 'b' within one instance of a class, the variable 'a' will also be set to 'b' in all other instances as well.

In OOP, there are multiple instances of a particular class. This means that each variable is kept different from the other instances. So, if we were to set variable 'a' in one instance of a class to 'b', the variable 'a' will still be 'a' within other instances of the class. Get it?

Step 4:
You may of noticed that I have put in a line to run a 'begin' void from our constructor in our Main.java class, lets make this now...

  1. public void begin(){
  2. ArrayList<Game> games = new ArrayList<Game>();
  3. Game cod = new Game("Call of Duty", "PS3");
  4. Game battlefield = new Game("BattleField", "Xbox 360");
  5. Game gta = new Game("Grand Theft Auto", "PS3");
  6. games.add(cod);
  7. games.add(battlefield);
  8. games.add(gta);
  9. for (int i=0;i<games.size();i++){
  10. System.out.println(games.get(i).title + " in the format of " + games.get(i).platform);
  11. }
  12.  
  13. //Change Cod platform from ps3 to xbox 360 and re-output...
  14. cod.setPlatform("Xbox 360");
  15. for (int i=0;i<games.size();i++){
  16. System.out.println(games.get(i).title + " in the format of " + games.get(i).platform);
  17. }
  18. }

So first we are creating a list of 'games' which consists of variables of the type 'Game' (our OOP class). We then create a few different instances of our Game class and add them all to our games list.

We then output each game within our games list to the console followed by changing the platform our 'cod' and re-outputting the information.

Project Complete!
That's it! Below is the full source code and download to the project files.

Main.java

  1. import java.awt.Dimension;
  2. import java.util.ArrayList;
  3. import javax.swing.JFrame;
  4. import javax.swing.JLabel;
  5.  
  6. public class Main extends JFrame{
  7. JFrame frame = new JFrame();
  8. public Main(){
  9. Dimension dim = new Dimension(720, 480);
  10. frame.setTitle("OOP Tutorial");
  11. frame.setSize(dim);
  12. frame.setPreferredSize(dim);
  13. frame.setMinimumSize(dim);
  14. frame.setMaximumSize(dim);
  15. frame.setLocationRelativeTo(null);
  16. frame.pack();
  17. frame.setVisible(true);
  18. begin();
  19. }
  20.  
  21. public static void main(String args[]){
  22. new Main();
  23. }
  24.  
  25. public void begin(){
  26. ArrayList<Game> games = new ArrayList<Game>();
  27. Game cod = new Game("Call of Duty", "PS3");
  28. Game battlefield = new Game("BattleField", "Xbox 360");
  29. Game gta = new Game("Grand Theft Auto", "PS3");
  30. games.add(cod);
  31. games.add(battlefield);
  32. games.add(gta);
  33. for (int i=0;i<games.size();i++){
  34. System.out.println(games.get(i).title + " in the format of " + games.get(i).platform);
  35. }
  36.  
  37. //Change Cod platform from ps3 to xbox 360 and re-output...
  38. cod.setPlatform("Xbox 360");
  39. for (int i=0;i<games.size();i++){
  40. System.out.println(games.get(i).title + " in the format of " + games.get(i).platform);
  41. }
  42. }
  43. }

Game.java

  1. public class Game {
  2. public String title = "";
  3. public String platform = "";
  4. public Game(String name, String plat){
  5. this.title = name;
  6. this.platform = plat;
  7. }
  8. public void setPlatform(String plat){
  9. this.platform = plat;
  10. }
  11. }

Output in the Console:

  1. Call of Duty in the format of PS3
  2. BattleField in the format of Xbox 360
  3. Grand Theft Auto in the format of PS3
  4. Call of Duty in the format of Xbox 360
  5. BattleField in the format of Xbox 360
  6. Grand Theft Auto in the format of PS3



Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

Lots of problems in this code:

- it's useless to extend JFrame if you are using another JFrame variable
- all swings processes should be run in the Event Dispatch Thread
- why opening a JFrame to output everything in the console
- ...

Publishing such bad code will only perpetuate bad habbits by new devs.

Add new comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
To prevent automated spam submissions leave this field empty.

Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.