Thursday, May 12, 2022

Using Java For Object Oriented Programming For Beginners

Using Java For Object Oriented Programming For Beginners

I find it easier to use Java programming language to write object oriented program, compare to Python and C++.

C++ is powerful, but I find it complicated. There are advantages using C++. GNU GCC compiler support C++, you don’t have to install another bloated Java C compiler and Java runtime.

Python is easy to learn and use, but the object oriented feature is limited.

I am using the classic example, get the area of a shape, circle and rectangle.


Python
$ edit shape.py
from math import pi
class shape:
    ''' class shape '''
    def __init__(self):
        pass

    def get_area(self):
        return 0

class rectangle(shape):
    def get_area(self,height,width=0):
      return height*width

class circle(shape):
    def get_area(self,radius):
        return pi*radius**2

print(rectangle().get_area(5,4))
print(circle().get_area(5))
Java
abstract class Shape {
	abstract int getArea();
}

class Rectangle extends Shape {
// implement nothing here, will generate error
}
$ javac Rectangle.java
==> error

In Python, if you don't implement rectangle().get_area(), there is no error. The rectangle().get_area() will always return 0 as stated in shape().get_area(). In Java, the compiler error tell you to implement getArea() in subclass.


$ edit Rectangle.java
class Rectangle extends Shape {
	int getArea() { return 0; }

	int getArea(int height, int width) {
		return height*width;
	}
}
  

Polymorphism

Square is a polymorphism of rectangle. They looks the same, the different is square has the same height and width.

Python
$ edit shape.py # add in the code
...
class rectangle(shape):
    def get_area(self,height,width=0):
        if width==0:
            return height*height
        return height*width

sq= rectangle()
print(rectange().get_area(5,4))
print(sq.get_area(4))
...
  

I use a if statement to determine the diffrent code for rectangle and square. The code will look messy if the implementation for rectangle and square are complicated. In java, you can split into 2 methods, like this:


Java
$ edit Rectangle.java

class Rectangle extends Shape {
	int getArea() { return 0; }

	int getArea(int height, int width) {
		return height*width;
	}

	int getArea(int height) {  // this is for square
		return height*height;		
	}
}
  

Polymorphism 2: Waterworld

Let's says the mutant has lungs and gills to breath above water and under water.


$ edit waterworld.py
class air:
	def __init__(self):
		pass

class water:
	def __init__(self):
		pass

class mutant:
	def __init__(self):
		pass
	def breath(self,media):
		if type(media)==air:
			print('use lungs')
		elif type(media)==water:
			print('use gills')
		else:
			print('can not breath')

mariner=mutant();
for m in (air(),water()):
	mariner.breath(m)
  
Java

How do you implement in Java? Tips: create Java object Air and Water. Create 2 methos in Mutant object: 1. breate(Air) 2. breate(Water).