Java For Python Developers 2 – OOP

This blog will focus on Object Oriented Programming concepts like Inheritance, Polymorphism and writing Packages, Interfaces and Abstract classes in Java.

Packages

Packages make a project organized. It creates a level of security by letting only the classes within that package have access, and it also provides name-scoping.

  • Some of them we have already come across are System (System.out.println), String, Math. They are from the package java.lang that is automatically included without having to import.
  • An import statement is written at the beginning of the code and is accessible throughout. An alternate way is to type the full name on each use in the code., i.e <pkg_name>.<class_name>
  • Java doesn’t compile the imported class. An import statement only saves time typing the full name of every class.

Inheritance

Public, Private, Protected and Default are called access controls. They control whether a variables or a methods can be inheritance or not.

  • Public members are inherited.
  • Private members are not inherited
  • Protected members can be access only by subclasses
  • Default can be accessed only within the same class. It doesn’t require a keyword.

Java does not support multiple inheritance, as it leads to the “Deadly Diamond of Death” problem. Lets see what that means.

180px-Diamond_inheritance.svg

The diamond problem is an ambiguity that arises when two classes B and C inherit from A and then when D inherits from B and C. Now, if there is a method in A that has been overridden by both B and C then which version of the method does D inherit? that of B or that of C? To avoid this problem, Java does not allow multiple inheritance.

Few other important aspects of inheritance:

  • A non-public class can be sub-classed only by the classes in the same package.
  • A class declared as final cannot be sub-classed anymore.
  • A class which has all private constructors cannot be sub-classed.
  • The keyword used is “extends”

Polymorphism

Polymorphism is having many forms of a method.

It is not a valid overloading if only the return type of a given method has been changed. To overload a method, the argument list has to be changed.

Abstract Class

  • Abstract Classes cannot be instantiated. The compiler will restrict instantiation of an abstract class. Whereas, concrete classes can be instantiated.
  • Abstract Methods must be overridden by the inherited class. Abstract methods do not have a body. It keeps a check to ensure that the inherited class implement them.
  • An abstract method should always be put in an abstract class.

Lets see how they are implemented in Java:

abstract class Pan extends Utensils { 
    public abstract void Material(); //an abstract method.
} 

Pan p; //works
p = new Spoon; //works
p = new Pan; //Compiler throws an error: Pan is abstract; cannot be instantiated

It is similar to that in Python. Lets see the implementation:

from abc import ABCMeta, abstractmethod #IMPORTANT

class animal:
     __metaclass__ = ABCMeta
    def delegate(self):
        self.action()

    @abstractmethod
    def action(self): pass

class tiger(animal): pass

A=animal()
t = tiger()

Interfaces

An interface can be presumed to be an extra feature that are not inherited from a parent class. This kind of helps overcome the drawback of not being able to inherit multiple classes.

  • An interface contains all “abstract” methods that the subclass is forced to implement. It can be perceived as a 100% abstract class
  • The keyword used is “implements”
  • Interfaces are always “public”. Methods in the interface are always “abstract”
  • A class can implement multiple interfaces which allows adding a number of features to a class making it more meaningful, in other words more real-word than the parent class.

Here’s an example:

interface Pet { 
    // all are the abstract methods. 
    abstractvoid wagTail(int a); 
    abstractvoid RunAround(int a); 
    abstractvoid EatFood(int a); 
}

class Dog extends Animal implements Pet {}

Conclusion:

In this blogpost we learnt some Object Oriented Programming concepts in Java. We also learnt how to implement Packages, Abstract Classes and Interfaces. In the next blog will be a deep dive into Memory management and Garbage Collection.

The previous blog was about getting started with basic concepts like using Variables, write Classes, conditional statements, loops and built-in libraries.                                     Java for Python Developers 1 – Basics

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