Java Tutorial: Object Oriented Programming
Object oriented programming is a type of programming which uses custom data types (classes) which has certain data fields (variables) and operations (methods). Object oriented programming allows programmers to use these classes to create instances (objects) that can have properties of the class template.
Classes & Objects
A class is a template with methods and properties objects are based off of. Every object is made from a class and will have access to the methods from that class. Classes can help stop repetition of code in your programs.
In this example we will be making a class representing a rectangle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
In the code above, we made our rectangle class which has 2 properties/fields; width and length, a constructor, and 3 methods. The constructor is a special method used to initialize objects of the class. The constructor in this class is
new Rectangle(double l, double w). Now let’s make some Rectangle objects.
1 2 3 4 5 6 7 8
In the runner class above, we make two rectangles; one with dimensions of 10x5 and another with 5x5. We then run their
toString() methods in the println. The output should be the following:
This shows that we have made the two rectangles with their dimensions and the program calculated the area of each object which is 50units2 and 25units2
Inheritance is when one class acquires methods or fields of another class. A class in java can only inherit from one other class. The class that inherits is known as the subclass and the class that the methods and fields are inherited from is known as the superclass.
Continuing our example from above, we will be making a Square subclass because squares are rectangles.
1 2 3 4 5 6 7 8
In this new class we have a new constructor that takes one argument because squares have equal side lengths. We then run
super(l,l) so that we can pass data to the superclass’s constructor so we can use the fields and methods in the superclass. However, in this class we make a change to the area method to make it use exponents instead of multiplication. This is known as an override and it replaces the
area() method in the Rectangle superclass for one specific to the Square class.
We can create objects of the Square class and use its methods just like the Rectangle class
1 2 3 4 5 6 7
Upon running this program we get this output:
This shows that we have create a square of side length 10 and an area of 100units2
Polymorphism is when an object takes on other data type forms. One case where polymorphism occurs is when a subclass object is used instead of a superclass object. This is also possible with interfaces. To have polymorphism, the object needs to pass more than one IS-A test of the superclass. For example:
A Square IS-A Rectangle
Because this test is true, any object of the Square class can be used instead of an Rectangle object.
Instance vs Static
Static methods or variables can be accessed from the class without creating an object. Some examples of this are methods from the String and Math classes. Instance methods or variables can only be used after creating an object. Static methods can be created by adding the static keyword to the method header.