Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Samuel Finocchio
- * Object Orientation basics
- */
- // This is the definition of a class
- // class is the keyword used to define ObjectOrientationBasics as a class
- public class ObjectOrientationBasics {
- // Here attributes (fields) should be defined
- // Attributes should be declared as private
- // private means that you can't have direct access to the member of the class
- private String attribute;
- // So you can't do something like this:
- // ObjectOrientationBasics.attribute (DOES NOT WORK)
- // ObjectOrientationBasics oob = new ObjectOrientationBasics();
- // oob.attribute (DOES NOT WORK) it is private
- // Public defines attributes visibile outside the local context
- public String publicAttribute;
- // So you can do this
- // ObjectOrientationBasics oob = new ObjectOrientationBasics();
- // oob.publicAttribute; (DOES WORK)
- // In a static context you can reference an attribute directly from the class itself
- public static final double PI = 3.14159;
- // So this works:
- // ObjectOrientationBasics.PI (DOES WORK);
- // But you have no power on it
- // ObjectOrientationBasics.PI = 1.1618; (YOU CAN'T DO THAT)
- // So at the end..
- // visibility modifier - specification of the context (static or not) - type - name;
- // When calling new ClassName();
- // We are calling a constructor method, that means that we are defining the object with a default object of that class type.
- // We can create a constructor like this
- // It MUST have the same name as the referencing class
- public ObjectOrientationBasics() { // It does not have a specified return type
- // We use the constructor to define the default values of the class
- this.publicAttribute = "Hi, i am public";
- this.attribute = "And i am private";
- // This way when we create a new object these attributes will have these strings as default value
- // ObjectOrientationBasics oob = new ObjectOrientationBasics(); // new calls the constructor
- // System.out.println(oob.publicAttribute); // This will print "Hi, i am public" without assigning anything to it
- }
- // Other methods should be defined here
- // Usually methods are defined as public
- // This is something called a get method, it allows to work with private attributes outside the local context
- public String getAttribute()
- {
- return attribute;
- }
- // This is called set method, it allow to edit a private attribute outside the class
- public void setAttribute(String attribute) {
- // this references the class it self so it means that attribute is the one defined on top up there
- this.attribute = attribute;
- }
- // Other random method
- public void sayHi() {
- System.out.println("HI!!");
- }
- // This is method overload, it uses the same name but different attributes and a different code
- public void sayHi(String name) {
- System.out.println("Hi, " + name + "!!");
- }
- // The constructor can be overloaded too
- public ObjectOrientationBasics(String attribute) {
- this.attribute = attribute;
- }
- // This is a different constructor in witch we can specificy our own default value to attribute
- // ObjectOrientationBasics oob = new ObjectOrientationBasics("Someting else");
- // System.out.println(oob.getAttribute()); // prints "Something else"
- // If we call sayHi(); "HI!!" appears
- // if we call sayHi("John") "Hi, John!!" appears
- // A static method can be called without an object
- public static double average (int a, int b) {
- return (a + b) / 2;
- }
- // This can be done
- // ObjectOrientationBasics.average(4, 9);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement