Advertisement
informaticage

ObjectOrientationBasics

Nov 29th, 2016
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. /**
  2.  * @author Samuel Finocchio
  3.  * Object Orientation basics
  4. */
  5. // This is the definition of a class
  6. // class is the keyword used to define ObjectOrientationBasics as a class
  7. public class ObjectOrientationBasics {
  8.     // Here attributes (fields) should be defined
  9.     // Attributes should be declared as private
  10.     // private means that you can't have direct access to the member of the class
  11.     private String attribute;
  12.     // So you can't do something like this:
  13.     // ObjectOrientationBasics.attribute (DOES NOT WORK)
  14.     // ObjectOrientationBasics oob = new ObjectOrientationBasics();
  15.     // oob.attribute (DOES NOT WORK) it is private
  16.    
  17.     // Public defines attributes visibile outside the local context
  18.     public String publicAttribute;
  19.     // So you can do this
  20.     // ObjectOrientationBasics oob = new ObjectOrientationBasics();
  21.     // oob.publicAttribute; (DOES WORK)
  22.    
  23.     // In a static context you can reference an attribute directly from the class itself
  24.     public static final double PI = 3.14159;
  25.     // So this works:
  26.     // ObjectOrientationBasics.PI (DOES WORK);
  27.     // But you have no power on it
  28.     // ObjectOrientationBasics.PI = 1.1618; (YOU CAN'T DO THAT)
  29.    
  30.     // So at the end..
  31.     // visibility modifier - specification of the context (static or not) - type - name;
  32.    
  33.    
  34.     // When calling new ClassName();
  35.     // We are calling a constructor method, that means that we are defining the object with a default object of that class type.
  36.     // We can create a constructor like this
  37.    
  38.     // It MUST have the same name as the referencing class
  39.     public ObjectOrientationBasics() { // It does not have a specified return type
  40.         // We use the constructor to define the default values of the class
  41.         this.publicAttribute = "Hi, i am public";
  42.         this.attribute = "And i am private";
  43.        
  44.         // This way when we create a new object these attributes will have these strings as default value
  45.         // ObjectOrientationBasics oob = new ObjectOrientationBasics(); // new calls the constructor
  46.         // System.out.println(oob.publicAttribute); // This will print "Hi, i am public" without assigning anything to it  
  47.     }
  48.    
  49.     // Other methods should be defined here
  50.     // Usually methods are defined as public
  51.    
  52.     // This is something called a get method, it allows to work with private attributes outside the local context
  53.     public String getAttribute()
  54.     {
  55.         return attribute;
  56.     }
  57.    
  58.     // This is called set method, it allow to edit a private attribute outside the class
  59.     public void setAttribute(String attribute) {
  60.         // this references the class it self so it means that attribute is the one defined on top up there
  61.         this.attribute = attribute;
  62.     }
  63.    
  64.     // Other random method
  65.     public void sayHi() {
  66.         System.out.println("HI!!");
  67.     }
  68.    
  69.     // This is method overload, it uses the same name but different attributes and a different code
  70.     public void sayHi(String name) {
  71.         System.out.println("Hi, " +  name + "!!");
  72.     }
  73.    
  74.     // The constructor can be overloaded too
  75.     public ObjectOrientationBasics(String attribute) {
  76.         this.attribute = attribute;
  77.     }
  78.    
  79.     // This is a different constructor in witch we can specificy our own default value to attribute
  80.     // ObjectOrientationBasics oob = new ObjectOrientationBasics("Someting else");
  81.     // System.out.println(oob.getAttribute()); // prints "Something else"
  82.    
  83.     // If we call sayHi(); "HI!!" appears
  84.     // if we call sayHi("John") "Hi, John!!" appears
  85.    
  86.     // A static method can be called without an object
  87.     public static double average (int a, int b) {
  88.         return (a + b) / 2;
  89.     }
  90.    
  91.     // This can be done
  92.     // ObjectOrientationBasics.average(4, 9);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement