If have login problems remove cookies and clear browser cache.



Post Reply 
 
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Java Lesson #1: Class and Interface - ¿What is polymorphism?
07-07-2021, 11:18 PM (This post was last modified: 08-29-2021 05:26 AM by JoseskVolpe.)
Post: #1
Java Lesson #1: Class and Interface - ¿What is polymorphism?
Introduction

There's many different kind of languages when we're speaking about programming languages. As we're speaking of Java, we should speak about object-orientation.
That'll be a huge reading, and there'll be a activity in the ending. So take your notebook and have a good time reaing it ;3


Like in many object-oriented languages, in Java you can extend another class's methods and variables in another class. That's called inheritance.

In the concept of inheritance, one class have their ancestor and descendents. A superclass and a subclass.
The superclass is the class that the current class comes from, who does it extends. It's the parent.
The subclass is the child, it's the class that extends the current class.

Methods and attributes

Methods are actions, they can be invoked, receive pointers and return values.
In Java, an attribute can be either an value or an pointer. Pointers are locations of another value in the RAM. Because pointers are implicit in Java, you don't need to worry to define either it's an pointer or a value like you must do in C language,

A attribute can have any kind of value, it can be a primitive or a Object, while an mehod can return any kind of value (a primitive or a Object), or return void. A primitive value can be a char, byte, short, int, long, boolean, double or a float.
  • char - A single letter. Character
  • byte - A 8-bit integer value (Occupy a single byte in RAM)
  • short - a 16-bit integer value (Occupy 2 Bytes in RAM)
  • int - a 32-bit integer value (Occupy 4 Bytes in RAM)
  • long - a 64-bit integer value (Occupy 8 Bytes in RAM)
  • boolean - a 1-bit value wich can be either true or false
  • double - A compacted floating value. It occupies less space in RAM but might be less precise.
  • float - A floating value


While it's not mandatory, they must have their visibility type. Wich can be a private, protected or public.
  • private: A method, or attribute, wich can't be visible by child classes
  • protected: A method or attribute that is visible only by the class and their childrens
  • public: A method or attribute that is visible by any class, these classes don't need to inherant the class they are from

Additionaly, a final method or attribute can't be modified or overwritten, doesn't matter their viibility. And a static attribute or method shares the same value/action to every Object, if a single Object, doesn't matter if it's the original class or a child, changes a static value, this value is applied to EVERYONE. If the attribute isn't a static attribute, these changes are made individually.

Polymorphism

In polymorphism, a child will extend every method and attribute from ther parents, and it's allowed to overwrite every non-final public or protected method and/or attribute.
Polymorphism allows developers to write a single class, or interface, to perform a single action in many different ways on different Objects.

In a game perspective, it could allow us to define a standard health meter, animation, actions etc to every NPC. In a zombie game, we could create a polymorphic tree of NPC's attributes, and make it easier to hurt, heal, kill, control etc any NPC from a Game Engine. For example:

[Image: hYtnkR0.png]
(If you don't see any image here, tell it)

In this concept, we have 3 different NPCs: Hero (wich is the player), Zombie and Big Zombie. We're free to create much more NPCs as we would like to create them too. All of them have the same attributes and methods from NPC: health, damage, x, y, defending, hurt(), heal(), attack(), move(), defend() and isDefending(). However, notice public isDefending() is a final class, it means this method can't be overwritten by their childs, but it can be accessed by anyone.
Because these methods and attributes aren't static, each constructed NPC will have their own value individually.

Notice every child (Hero, Zombie and BigZombie) overwritten health and damage attributes, also, both BigZombie and Hero have new methods. UseMidkit() will allow every Hero to use a medkit, and BigZombie has a curl() method, wich will allow every BigZombie to curl a Hero with their tongue.
BigZombie extends Zombie, wich extends NPC. That means BigZombie has the priviledge not to only extend every attribute from NPC, but also from Zombie class


Class vs Interface

A Class is a collection of attributes and methods. It's where a single Object is created from. It can also be used as a root for a polymorphic tree of classes, like on the last picture.
However, classes cannot extend multiple classes at once. That's what Interfaces are used for

Interface is a reference of methods, like it were a "draft". They give a collection of abstarct methods wich can be implemented by a Class. Abstract methods cannot define an actions, only classes wich implements them can define another action for them. If you implement another Interface into a Class, you must define their actions inside the Class that extends it.

Interfaces are often useful to be used in triggers.
As a example, we can use the last concept to demonstrate that. In a action game, we often want to give Items, Weapons etc to a player. We can create a Interface to be used as a trigger not only on the Player, but in anywhere of the game. In this concept, we want to both GameEngine and a single Hero to know when a Item has been used, equipped or destroyed.

[Image: 8VSecCq.png]
(If you don't see any image here, tell it)

Green are Classes, and Purple are Interfaces.
Notice that GameEngine and Hero are not from the same inheritance, but they share the same methods thanks to ItemListener interface, wich can be identified on assignListener() method on Item Class.
That's how useful Interfaces are. You can identify every Class that implement a Interface as the Interface, and assign them into a argument without the need to make them extend something. ¿That wouldn't make any sense to extend GameEngine into NPC, eh? Instead, you can just implement the same Interface on both of them.

Of course, there's MUUUUUCH BETTER ways to make Items in a game. That's just to demonstrate how you could use Interfaces to make stuff easier.

Activity

You'll not need to script anything on this activity

Take your paper and pencil, and concept a inheritance for that. Take a photo and upload here for me to evaluate you.

Make a inheritance graph for a racing game. On this racing game, you'll have a Player, polices and opponents. Everyone will have their own individual Vehicle, wich will need to receive actions if they get hit by someone, there'll be at least 2 diferent car models


[size=xx-small]Signature[/size]
[URL=https://www.exophase.com/user/JoseskVolpe/][IMG]https://card.exophase.com/2/0/45687.png?1612237163[/IMG][/URL]

[align=justify][size=x-small][b]OwO ¿¿¿What is this??? *wags tail*
Take my beret plz 7w7 ~[/b][/size][/align]
Find all posts by this user
Quote this message in a reply
07-07-2021, 11:27 PM
Post: #2
RE: Lesson #1: Class and Interface - ¿What is polymorphism?
(07-07-2021 11:26 PM)Fredyrr Wrote:  Well it's hard to learn

read it with calm

[size=xx-small]Signature[/size]
[URL=https://www.exophase.com/user/JoseskVolpe/][IMG]https://card.exophase.com/2/0/45687.png?1612237163[/IMG][/URL]

[align=justify][size=x-small][b]OwO ¿¿¿What is this??? *wags tail*
Take my beret plz 7w7 ~[/b][/size][/align]
Find all posts by this user
Quote this message in a reply
07-08-2021, 01:01 AM
Post: #3
RE: Lesson #1: Class and Interface - ¿What is polymorphism?
Cant cram..
Need a better time to read and understand.

how i want to net my enemies here in this forum.....***** the haters
Visit this user's website Find all posts by this user
Quote this message in a reply
07-08-2021, 01:14 AM
Post: #4
RE: Lesson #1: Class and Interface - ¿What is polymorphism?
Read it when you can. And don't forget to do the activity

[size=xx-small]Signature[/size]
[URL=https://www.exophase.com/user/JoseskVolpe/][IMG]https://card.exophase.com/2/0/45687.png?1612237163[/IMG][/URL]

[align=justify][size=x-small][b]OwO ¿¿¿What is this??? *wags tail*
Take my beret plz 7w7 ~[/b][/size][/align]
Find all posts by this user
Quote this message in a reply
07-08-2021, 04:23 AM (This post was last modified: 07-08-2021 04:24 AM by java gamer.)
Post: #5
RE: Lesson #1: Class and Interface - ¿What is polymorphism?
What Should We Do

Learning polymorphism or Drawing

[b][u][size=large] WHO HATES GAMING ,
They Are Already DEAD [/size][/u][/b]

[IMG]https://images.cooltext.com/5484671.png[/IMG]
Visit this user's website Find all posts by this user
Quote this message in a reply
07-08-2021, 08:10 AM
Post: #6
RE: Lesson #1: Class and Interface - ¿What is polymorphism?
(07-08-2021 04:23 AM)java gamer Wrote:  What Should We Do

Learning polymorphism or Drawing

That is part of scripting lessons. On here, you'll learn about polymorphism, the good and most important basic of Java and any other object-oriented language

[size=xx-small]Signature[/size]
[URL=https://www.exophase.com/user/JoseskVolpe/][IMG]https://card.exophase.com/2/0/45687.png?1612237163[/IMG][/URL]

[align=justify][size=x-small][b]OwO ¿¿¿What is this??? *wags tail*
Take my beret plz 7w7 ~[/b][/size][/align]
Find all posts by this user
Quote this message in a reply
07-08-2021, 08:57 AM
Post: #7
RE: Lesson #1: Class and Interface - ¿What is polymorphism?
(07-08-2021 08:10 AM)JoseskVolpe Wrote:  
(07-08-2021 04:23 AM)java gamer Wrote:  What Should We Do

Learning polymorphism or Drawing

That is part of scripting lessons. On here, you'll learn about polymorphism, the good and most important basic of Java and any other object-oriented language

What About Activity

[b][u][size=large] WHO HATES GAMING ,
They Are Already DEAD [/size][/u][/b]

[IMG]https://images.cooltext.com/5484671.png[/IMG]
Visit this user's website Find all posts by this user
Quote this message in a reply
07-08-2021, 09:21 AM
Post: #8
RE: Lesson #1: Class and Interface - ¿What is polymorphism?
(07-08-2021 08:57 AM)java gamer Wrote:  What About Activity


¡Do that! That will help you to learn this :3

[size=xx-small]Signature[/size]
[URL=https://www.exophase.com/user/JoseskVolpe/][IMG]https://card.exophase.com/2/0/45687.png?1612237163[/IMG][/URL]

[align=justify][size=x-small][b]OwO ¿¿¿What is this??? *wags tail*
Take my beret plz 7w7 ~[/b][/size][/align]
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)