Learning Programming #2.3 Learning Java: Object Orientation

Now that you know about Exceptions and Errors the real programming can start.

Java is an object orientated programming language. That means you can use and create objects of any kind.
Objects in programming are basically a collection of different functions and variables.
You can instantiate an object with the constructor using
new Object(constructorarguments);.
Every instance shares the same functionality and the same types of variables, but differs in the values of those variables.

There are a few special things that can be done with objects. I decided to first explain them and then give a code example:

Inheritance:

In this case one object(ususally referred to as Subclass) inherits all functionality and variables from another object(ususally referred to as Subperclass). So you can access the functions of the Superclass from the Subclass.
In java inheritance can be done with:
class SubClass extends SuperClass {…}
One object can only inherit from one other object!
You can store an Instance of SubClass as a variable of the SuperClass type:
SuperClass object = new SubClass();
This can be very useful when you have multiple different Subclasses, but want to store them in one array.
You can reverse the process by casting, but you should make sure the object is really an instance of the Subclass you cast it to:
SubClass subObject = (SubClass)object;
It is also possible to override functions of the superclass from within the subclass:

class SuperClass {
  int someFunction() {
    return 0;
  }
}
class SubClass extends SuperClass {
  // Overrides the functions, so it returns 1 instead of zero.
  @override
  int someFunction() {
    return 1;
  }
  public static void main(String[] args) {
    SuperClass objectSuper = new SuperClass();
    SuperClass objectSub = new SubClass();
    objectSuper.someFunction(); // Returns 0 because the function in superclass returns 0
    objectSub.someFunction(); // Returns 1! Although the object is stored as a superclass, the object is still a SubClass and therefor the function of SubClass is called.
  }
}

Interfaces:

An interface is also some kind of object, but it contains only function templates(containing return type, function name and arguments).
An interface can be created like this:

interface Interface { // Interface
  void aFunction(int arg1); // Function template
}

Interfaces can be implemented by a normal class while filling its functions with functionality:

class InterfacedClass implements Interface {
  // Needs to implement the functions of Interface:
  @override
  void aFunction(int argInt) { // The argument's names can be changed if you want.
    return; // Do nothing.
  }
}

Now the interfaced class can be stored and accessed using the interface type:
Interface inf1 = new InterfacedClass();
A class can implement as many interfaces as you want:
class MultiInterface implements Interface1, Interface2,… {…}

Coding Example:

A good example to show object orientation are animals. For most animals you can easily find some common functions and variables and put them in a superclass.
Screenshot from 2019-06-20 12-18-36.png

Sort:  

Congratulations @quantumdeveloper! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 100 comments. Your next target is to reach 200 comments.

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

The Steem community has lost an epic member! Farewell @woflhart!
SteemitBoard - Witness Update
Do not miss the coming Rocky Mountain Steem Meetup and get a new community badge!

You can upvote this notification to help all Steem users. Learn how here!

Coin Marketplace

STEEM 0.20
TRX 0.15
JST 0.029
BTC 63780.55
ETH 2618.13
USDT 1.00
SBD 2.82