Java - OOP Tutorial #2
Welcome to your next tutorial. This tutorial is going to cover object oriented programming in Java and inheritance of class functions with overrides!
A little story about object oriented programming and myself. When I was taking classes at University, and learned my first programming language, C. I was excited to learn programming. After a few months, we started learning structures. Now I was extremely happy because I envisioned what I wanted to make! Too bad object-oriented programming OOP is kind of bad in C because it does not support classes, and everything has to be coded from scratch unless you use libraries from other people. Now back to Java, where classes and objects are so much easier to code. It's also a fun process of thinking and designing how you want to design a class along with its properties, methods, routines, variables, accessors, mutators, default constructor, and custom constructor.
Java's way of defining classes is similar to defining structures in C, defining class templates in C++, classes in C#, and classes in Python. They just all have different syntax and available libraries. If you look at my Object Details X3 for C++, you will get a better understanding of objects, which is like Java.
Here are some extra links for you to learn OOP in Java.
In this tutorial I will also focus on inheritance. Inheritance means exactly what it is literally in programming. A quick example is a car. All cars have four wheels, but then in cars they can have different types of cars like a taxi car. Taxi car can inherit properties from car as well. This way we don't have to keep rewriting code. We can borrow and use whatever car has in taxi car. However, if there are private methods and variables you cannot access them directly from the subclass. Here is an intimidating code. It's not that difficult to understand even if you're a beginner.
Another nice source code I wrote for a homework assignment, lol.
The Code Output:
Below is the code output, which is needed for me to explain what is happening.
I will do my best to explain to you what is happening in this code in a minimalistic way. First off, I added some java libraries at the top and added comments. Next, we created the super class which is Dog, and the sub-class which is SuperDog. By adding the keyword extends this tells Java we want to inherit from the class Dog, which is the super class of SuperDog. Now each of them have there own variables, methods, routines, etc. If you understand how setters and getters works, they are essential for setting variables and getting variables if you do not want the user to directly access them. It is also a good practice because you can even filter out other inputs from the user. A good example of that is the Power and PowerUp() in the SuperDog class. An object cannot directly be written into it's Power property, They have to be set with the setPower(). Further down in the SuperDog class, you can see an Override of the function Speak(). That Speak() completely replaces the Dog's Speak() method. This is useful if you want to display information in different classes. Sometimes a sub-class may not need or have certain things another class has and you can override that exact function. This not only saves lines of codes, but also makes your code efficient.
Hopefully you can find this slightly helpful. Keep practicing coding and create many classes for your program because it is a great way to organize your work, especially for web applications and systems.
Comments powered by CComment