Classes and Objects

8 0 0
                                        

There is more to a class. It can extends another class from what we called a parent class or a super class. The class that extends is a child class or a sub class. This sub class can inherit all the fields and methods of the parent class. That is why there are built in packages created for us to use. These packages would make are programming life easier. But the rule is, it can only extends one super class. They made it like this to prevent further complications. There will be incompatibilities in the program. But to make up for it, they created an interface where you can call as many as you want to implement it in the class.

================================================================================

interface YourCondition{

           public boolean Testing(String x, int y);

}

class NumberOfPlayers implements YourCondition{

          private String name;

          private String teamName = "Alpha";

          private int numSize = 550;


         NumberOfPlayers(String title){

                   name = title;

        }

        public boolean Testing(String x, int y){

                 if(numSize > y){

                             return true;

                } else {

                            return false;

                }

          }

         public int getSize(){

                     return numSize;

         }

        public String getTeam(){

                  return teamName;

        }

}

class Marine extends NumberOfPlayers{

         private String marine = "Marine";

         private int marineSize = 300;

         int wonBy;


          Marine(String title){

                  super(title);

         }

       public void clashing(){

                if(Testing("marine", marineSize)){

                          wonBy = getSize() - marineSize;

                          System.out.println("The battalion named " + getTeam() + " won by " + wonBy);

                } else {

                        wonBy = marineSize - getSize();

                       System.out.println("The battalion named " + marine + " won by " + wonBy);

              }

        }

}

public class MoreOnClasses{

          public static void main(String[] args){

                   Marine m = new Marine("Battle of Numbers");

                   m.clashing();

           }

}

================================================================================

/*Output:

The battalion named Alpha won by 250

*/

================================================================================/* Meaning of the word or words */

1. "interface YourCondition{} " - This is the interface. It is like declaring a class but you just put a word interface instead of class.

2. "public boolean Testing(String x, int y);" - inside the body of interface is this method. A method without a body. Instead of having braces and statements, you ended it right away with the semi colon. It is an abstract method.


3. class NumberOfPlayers implements YourCondition{} - this interface named "YourCondition" was implemented in the class named "NumberOfPlayers" using the word "implements"

4. public boolean Testing(String x, int y){

         if(numSize > y){

               return true;

          } else {               return false;         }


   You see, if you implement the interface, you must implement that method inside it or declare your class as an abstract and make another class to implement that interface method.

5. "class Marine extends NumberOfPlayers{}" - This class named Marine extended to a class named "NumberOfPlayers". "Marine" is now a sub class or a child class of the super class "NumberOfPlayers". Marine can inherit the fields and methods of the super class but if the field is declared private, you need to create a set and get method that is accessible as public in the super class for the sub class "Marine" to access your private field or variable. In this sub class, we access the method named "Testing" and the field where we use a get and set method, named "getSize()" and  "getTeam()" to be use in our method named "clashing".

6.  super(title); - This super  keyword functions like this keyword. I don't if i have mentioned it before. This super keyword should be the first declaration in your constructor of the subclass in case you want to access the parameter of the constructor of the super class.

Note: The compiler will create a default constructor for you if you forgot to make one for class.



Java codes i learned onlineWhere stories live. Discover now