Essay Available:
You are here: Home → Other (Not Listed) → IT & Computer Science
Pages:
7 pages/≈1925 words
Sources:
7 Sources
Level:
APA
Subject:
IT & Computer Science
Type:
Other (Not Listed)
Language:
English (U.S.)
Document:
MS Word
Date:
Total cost:
$ 37.8
Topic:
Active Comparison of Java and Python Programming Language (Other (Not Listed) Sample)
Instructions:
An active comparison of java and python programming language (with executable codes)
source..Content:
COURSEWORK FOR PROGRAMMING PARADIGMS
By (Name)
The Name of the Class (Course)
Professor (Tutor)
The Name of the School (University)
The City and State where it is located
The Date
Introduction
Python is a strong and powerful modern day computer programming language, it is seen to draw close similarities with earlier programming languages such as FORTRAN, however, it is much powerful than this early programming language. Moreover, python is a high-level, interactive, interpreted and object oriented language. Why so? First, it’s interpreted because it is processed at runtime by a given interpreter without the need of a compiler. Secondly, it is an interactive language that enables the user to interact with the interpreter as he/she writes the code. Thirdly, python is an object-oriented language since it supports object oriented techniques that encapsulate codes into objects (class). Finally, it’s a beginner-level programming language that can develop a wide range of applications (Tutorial points 2014).
Nevertheless, python-like stated before holds several similarities and differences with other programming languages (both old and modern). For instance, its interpreted features are similar to those of PERL and PHP, which are also instances of object-oriented languages. However, this paper will analyze python as a programming language while comparing it with another OOP language, Java. Moreover, their syntax will be highlighted to provide a deeper understanding of the language, more so on inheritance (Doty 2008).
Python vs. Java
Basic comparisons
Let’s start with the basic, a simple syntax of a “Hello, World!†program that will help illustrate the two programs.
Python: print “Hello, World!†OR print (“Hello, World!â€) #this is for python 3
Java: public class HelloWorld
{
Public static void main (String [] args)
{System.out. println (“HelloId!â€);
}
}
From this simple programs it’s clear to see the main differences between these two languages, for python, it is an object-oriented language, however, not all of its instances are OOP. In fact, it is possible to write a program without any object-oriented techniques or concepts as seen above. On the other hand, Java is completely object-oriented i.e. it can only support OOP techniques as clearly shown above.
Python is dynamically typed to produce the required results, a good illustration of this is the introduction of variables. For one to introduce a variable, a specific value must be assigned to it. Example: saidVariable = 100. Furthermore, the value assigned to the variable can be changed later (not fixed). Example: saidVariable = 100, this can change to saidVariable = ‘Hello, World!’ On the other hand, Java is the exact opposite of python whereby it’s statically typed. Referring to the same example of a variable, a variable is first declared before assigning a value to it. Moreover, the variable is explicitly declared with the same type as the value it’s assigned. Example: int saidVariable; int saidVariable = 100;. Finally, a variable declared as a certain type cannot be assigned another value that is different from the declared type (Yang 2016).
Nevertheless, python is also similar to Java more so when considered from an ideology point of view. For one, they are both strong cross-platform languages that require runtime on a programmers system. Moreover, there is the obvious OOP implementation as well as collections similarities (sects, lists, and dicts). However, like seen above and in other many instances their syntax is significantly different but, when analyzed the python syntax and its features such as those of lists comprehensions are similar to those of Java.
Inheritance in both Python and Java
Inheritance in this scenario represents the ability of a class or even object to being defined as an extension of a different class or object. In python, all classes created technically use inheritance in that all classes are in fact subclasses of special classes called object(s). In essence, when not explicitly defined (inheritance) a class in a python program will automatically inherit from the object defined (Voegele 2016). However, as seen in the syntax below one can state this notation openly in the defining code.
Example: class MySubClass(object):
Pass
This simple description is inheritance at its core, a super class or otherwise known as parent class is the ‘original’ class that another inherits. As stated before, in python if another superclass is not defined, a class automatically inherits from the object defined. A subclass, on the other hand, is the class that inherits from the parent class.
Syntax example: from the basic class DerivedClass(ParentClass):
body_of_the_derived_class
class polygon:
def __init__(self, number_of_sides):
self.n = number_of_sides
self.sides = [0 for i in range(number_of_sides)]
def inputSides(self):
self.sides = [float(input("Enter side "+str(i+1)+" : ")) for i in range(self.n)]
def dispSides(self):
for i in range(self.n):
print("Side",i+1,"is",self.sides[i])
In this example, different attributes are provided that hold the data to store the number of sides (n). Moreover, the magnitudes of each side are given as a list (sides). From this base class, we can define another class e.g. triangle that can inherit from this base class.
class Triangle(Polygon):
def __init__(self):
Polygon.__init__(self,3)
def findArea(self):
a, b, c = self.sides
As shown above, the class triangle inherits the attributes of the parent class polygon i.e. n for the number of sides etc. In addition to this, we can also define another method that eventually calculates the area of the said triangle. Therefore, despite us not defining this method in the original base class, the final program will execute the code, giving the correct value. Python is able to search for unknown attributes or methods within a given class recursively until a match is found. This is the basis of multiple inheritances, a feature seen in python and not in Java as shown below.
However, regardless of the availability of multiple inheritance i.e. multiple parent classes (and lack of) minimal instances of this feature are recommended as an over use could lead to inheritance problems such as the diamond problem. Diamond problem occurs when multiple inheritances is structured poorly and when a class inherits the wrong method or attribute of a different class. Consider the example below:
class Sport:
    def athlete(self):
        pass
Â
class Football(Sport):
    def athlete(self):
        print("Ronaldo")
Â
class Basketball(Sport):
    def athlete(self):
        print("Lebron")
 class Champions(Football, Basketball):
    pass
 g = Champions()
g.athlete()
Because python uses a resolution order method to find an attribute for a given situation, reversing Basketball with Football (class Champions(Basketball, Football)) would see the output of the first class as Lebron instead of Ronaldo. To solve such problems python uses implicit behaviour to structure its code unlike Java that completely disallows multiple inheritance (Hugunin 1997).
Java inheritance
Inheritance in java is similar to python, however, each new classes (inherited classes) are created from existing classes i.e. the superclass. Therefore, unlike python a parent class must exist for an inheritance to occur. Furthermore, one can reuse all the methods used in a given parent class as well as its fields. New methods and fields can be added to the existing system however, multiple inheritance features are completely absent.
General syntax: class Subclass-name extends Superclass-name
{
//methods and the fields
} //note the superclass must be explicitly defined.
Extends is a keyword that is used to indicate that a new class is being derived from an existing superclass. Let’s highlight a sample code that utilizes inheritance and analyze its features. Below a simple Java program having two classes; calculator and my_calculator.
class Calculator
{
int z;
public void addition(int x, int y){
z=a+b;
System.out.println("The sum of the given numbers:"+z);
}
public void Substraction(int x, int y){
z=a-b;
System.out.println("The difference between the given numbers:"+z);
}
}
public class My_Calculator extends Calculator{
public void multiplication(int x, int y){
z=a*b;
System.out.println("The product of the given numbers:"+z);
}
public static void main(String args[]){
int x=20, y=10;
My_Calculator demo = new My_Calculator();
demo.addition(x, y);
demo.Substraction(x, y);
demo.multiplication(x, y);
}
}
This program will output the sum, difference, and product of the two numbers provided. However, a deeper analysis of the code indicates the following: One, inherited classes have only one parent class. This observation is not a coincidence to this particular program but is the rule of Java inheritance, whereby a cla...
Get the Whole Paper!
Not exactly what you need?
Do you need a custom essay? Order right now:
Other Topics:
- Answering Programming Questions I.E. Practical CodingDescription: Write a CGI program that displays a count of how many times a browser on each computer has contacted the server...2 pages/≈550 words| 3 Sources | APA | IT & Computer Science | Other (Not Listed) |
- The Type Of Creative AI Idea: Super Smart MachineDescription: Artificial Intelligence idea and thus this sample provides details about the Artificial intelligence idea and how it works, its advantages and disadvantages...1 page/≈550 words| 1 Source | APA | IT & Computer Science | Other (Not Listed) |
- A Project Design Proposal On A Grocery Store SystemDescription: Provide a brief but comprehensive design of an expressive interface that generally will offer customers the ability to carry out their grocery shopping online...2 pages/≈550 words| 5 Sources | APA | IT & Computer Science | Other (Not Listed) |