OOP

Object Oriented Programming

Java is an Object-Oriented Programming language. It utilizes objects and classes to run applications.

An object is a piece of software with attributes in the form of variables.
It is an instance of a class, and must be created before it is called or executed.
To create an object, a constructor is used. The constructor has the same name as the class and can take parameters.
The new keyword is used to create an object with a constructor.

A method is the behavior of an object. When methods are run, the object performs an action. Methods can take parameters, return values, both, or neither. A method with no return value is declared as void.

A variable is a specific attribute or characteristic of an object. Variables can be private or public, with private variables being inaccessible outside of the class they are in.

To access an object's variables and methods, dot notation is used. This is in the form objectName.variableName or objectName.methodName.

          
    public class HelloWorld {	// class HelloWorld which creates HelloWorld object

      private int exampleInt;	// private attribute of the HelloWorld object
            
      public HelloWorld {	// HelloWorld Constructor Method
            
      }
            
      public void exampleMethod(int x){	// method of object HelloWorld with parameter int x and return type void
        exampleInt = 0;
      }
              
      public static void main(String[]args){
        HelloWorld h = new HelloWorld(); // creating a new HelloWorld object using the constructor method
        h.exampleMethod(1);	// calling the exampleMethod method of object h with dot notation
      }

    }
        


Inheritance

In OOP, there exist subclasses derived from parent classes.
These subclasses are written in the notation public class subClass extends parentClass.

The extends keyword allows the subclass to inherit all the methods and properties of the parent class. The subclass can override the methods of the parent or superclass by redeclaring the method with the same name. Additionally, the subclass can further have its own properties and methods specific to that subclass. Variables and/or methods private to subclasses are said to be encapsulated.

The super keyword allows the subclass to call methods or constructors from the superclass.

          
    public class class1(){
      public void method1(){
        System.out.println("Hi");
      }
    }
            
    public class class2 extends class1(){
      public void method2{
        super.method1();	// calls method1 from the superclass and prints "Hi"
        System.out.println("Hi again");
      }
    }
        

Set / Get Methods

Set / get methods are used to access or change attributes which are encapsulated. This protects the variables without directly altering them.

          
    private int x; // private variable

    public int getX(){ // this method allows x to be accessed outside of its class
	    return x;
    }

    public void setX(int val){ // this method allows x to be changed outside of its class
	    x = val;
    }
      

"This" Keyword

The this keyword is used to distinguish between variables or methods which have the same name. Using this for a variable or method refers to the variable or method in the current class, as opposed to the parent.

          
    public class Class1{
      private int x = 0;
            
      public Class1(){
        System.out.println("Hi");
      }
          
      public Class1(String str){
        this();  // prints "Hi"
        System.out.println(str);
      }
    }
        


Polymorphism

Constructors and methods in Java may be overloaded or overridden.

Overloading means that multiple instances of a constructor or method may be declared with the same name but different parameters. Parameters may differ in number or type, but must be named the same.

Overriding means that a subclass can redefine a previously existing method from a parent class.

At runtime, Java will find the appropriate method version to use.

For example, here are three overloaded methods with the same name:

          
    public void overloadedMethod();

    public void overloadedMethod(int x);

    public void overloadedMethod(String x, boolean y);
        

Note that the name of the first overloaded parameter is always x.

Overloading allows the same method to be called with different parameters. This can be used to create a default constructor.

Basic Syntax

Class names must begin with a capital letter and then follow camelCase.

Method and variable names must begin with a lowercase letter and then follow camelCase.

          
    public class HelloWorld

    public void exampleMethod
        


Tips

Classes, methods, and variables cannot be named reserved words such as int, void, etc.

Use simple, short names which still allow the purpose of the class, method, or variable to be identifiable.


The Basics Inheritance Polymorphism Syntax Tips