Basic OOP concepts in Java

Posted: July 7, 2014 in Uncategorized
Tags: , ,

I know this is too basic, but, sometimes we need to check this basic things out to remember what we are using and why. So here it is my 50 cents about basic Object Oriented Programming concepts. Basically:

INHERITANCE
ENCAPSULATION
POLYMORPHISM
ABSTRACTION

INHERITANCE

  • Superclass: abstracts out common features of other classes
  • Subclass:
    • inherits fields and methods from superclass, except the private ones
    • overrides(existing methods) or extends(create own methods) methods from superclass.
    • a subclass can override a method from the superclass provided that it is not final (if it is static cannot be overridden but redeclared) [*see note below]
    • define changes from superclass
    • Java supports only single inheritance
    • super: invoking a superclass version of an overridden method
    • protected is like private but subclasses can inherit from them
public class Animal {...
public class Dog extends Animal{...

 

ENCAPSULATION

  • Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods (getter and setter methods)
  • Do not set the value for a field referencing directly, use getters and setters

 

POLYMORPHISM

Is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object (all Java objects are polymorphic for their own type and for the class Object).

public class Dog extends Animal implements Carnivorous{}
Dog d = new Dog();
Animal a = d;
Carnivorous c = d;
Object o = d;

All the reference variables d, a, c, o refer to the same Dog object in the heap.

Animal d = new Dog();

Reference type of d is Animal (compile time checks reference types)
Object type of d is Dog (runtime figures out the object type)
We can create an Array of objects of the same reference class but different object classes.

VIRTUAL method is the one overridden in the subclass and invoked like this:

d.bark();

compiler sees bark() in Animal to validate it but in runtime the method in Dog is invoked.

Object d cannot reference methods defined in Dog but not in Animal (unless we use a cast)

(Dog) d.methodFromDogNotInAnimal();

 

ABSTRACTION

ABSTRACT CLASSES

  • An abstract class is one that cannot be instantiated. You just cannot create an instance of the abstract class. They can be extended.
  • An abstract method consists of a method signature, but no method body.
  • Not all the methods have to be abstract, but the abstract ones need to be overridden by the classes extending them.
  • There are no abstract fields.
  • You can have static methods.
abstract public class Creature {
    protected String name;
    public abstract void setName();
...

public class Giraffe extends Creature{
...

 

INTERFACES

  • Are classes with only abstract methods (you don’t need to put abstract, they are by default)
  • One class can implement many interfaces, thereby inheriting the abstract methods of the interface.
  • Only uses public static and final fields
  • Classes from different inheritance trees can implement common interface.
  • TAGGING INTERFACE: interface with no methods, purposes:
    • create a common parent
    • adds a data type to a class
public interface Living {
public void setName(String newName);
public String getName();
...

public class Monkey implements Living{
...

 

tip of the day: Everything in Java is passed by value except for objects (passed by reference).

 

* Note – Example of redeclaring an static method of a superclass:

public class Cat extends Animal {
    public static void hide() {
        System.out.format("The hide method in Cat.%n");
    }
    public void override() {
        System.out.format("The override method in Cat.%n");
    }

    public static void main(String[] args) {
        Cat myCat = new Cat();
        System.out.println("Create a Cat instance ...");
        myCat.hide(); 
        Cat.hide();
        myCat.override();
        
        Animal myAnimal = myCat;
        System.out.println("\nCast the Cat instance to Animal...");
        myAnimal.hide();   //BAD STYLE
        Animal.hide();     //Better!
        myAnimal.override();

        Animal myAnimal1 = new Animal();
        System.out.println("\nCreate an Animal instance....");
        myAnimal1.hide();   //BAD STYLE
        Animal.hide();     //Better!
        myAnimal1.override();
    }
}
The output results are:

Create a Cat instance ...
The hide method in Cat.
The hide method in Cat.
The override method in Cat.

Cast the Cat instance to Animal...
The hide method in Animal.
The hide method in Animal.
The override method in Cat.

Create an Animal instance....
The hide method in Animal.
The hide method in Animal.
The override method in Animal.

 

Comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s