OO concepts refresher

Written on 5 November 2013, 12:19pm

Tagged with: ,

Going back to square one 🙂

Class: the blueprint from which individual objects are created.
Object: the instance of a class. The objects have state (fields) and behavior (methods).
Encapsulation: Hiding internal state and requiring all interaction to be performed through an object’s methods (getters and setters). Note: this requires that the class fields are private (“In the spirit of encapsulation, it is common to make fields private. […] Use private unless you have a good reason not to.“)
Inheritance: Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. Subclass extends Superclass

This keyword: can be used either for a field (this.radius=radius) or for a constructor – this(0,0)

Class variables (static fields): they are common to all objects. There is only a copy of this variable, no matter how many times the class is instantiated.
Instance variables (non-static fields): they are distinct for each object. Each object has its own copy.
Example: Bicycle class, numberOfGears static field, speed non-static field. Each Bicycle object has its own speed (instance variable), but all of them have the same number of gears (class variable).

Instance methods: they need an instance of a class. instanceName.methodName(args)
Class methods: they can be used without an instance of the class. ClassName.methodName(args)
Class vs instance methods: Instance methods can access instance variables, instance methods, class variables and class methods directly.
Class methods can access class variables and class methods directly. But they cannot access instance variables or instance variables directly (they must use an object). They also cannot use the ‘this‘ keyword

Constants: static + final modifiers. Final indicates that the value cannot change:
static final double PI = 3.141592653589793;

Method overloading: Methods within a class can have the same name if they have different parameter lists. Warning: The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
public void draw (int i);
public void draw (double d); //ok
public int draw (int i);
public void draw (int i); //compiler error

Method overriding: An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass’s method.
Method hiding: If a subclass defines a class method with the same signature as a class method in the superclass, the method in the subclass hides the one in the superclass.
Note: You will get a compile-time error if you attempt to change an instance method in the superclass to a class method in the subclass, and vice versa.

Super: If your method overrides one of its superclass’s methods, you can invoke the overridden method through the use of the keyword super: super.printDescription()

Polimorphism: Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.
public void printDescription() {
System.out.println("The MountainBike has a" + getSuspension() + " suspension.");

Interface: a group of related methods with empty bodies. A class implements an interface
Package: a namespace that organizes a set of related classes and interfaces.

Abstract classes: cannot be instantiated, but they can be subclassed.
Abstract method: is declared without an implementation:
abstract void moveTo(double deltaX, double deltaY);
Abstract classes vs interfaces: Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods. Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation.

Photo: instagram

Leave a response