Friday, March 4, 2016

Encapsulation and Polymorphism

Encapsulation is a term in Java programming and in general programming that refers to the privatization of files for classes and encapsulation of packages, there are different encapsulation key words: public, private and protected. they all do different things to your variables, classes, or methods. the public word allows to access a class, variable, or methods from one another if they are imported. The private keyword does not allow the interchange of classes, methods or variables even if they are imported. And the protected keyword only allows for methods, classes and variables to be used only within the same package. The way to import packages in any IDE from one class file is the keyword 'import' followed by a space. Ex: import java.util.*; the java.util library imports many very useful classes to the class to which you use this on, such as Random which has methods that create random numbers, or the Scanner class which reads user input from the console and gives it to a variable.
Plymorphism is a way to import classes in Java and assign them to another class as a child of that class, the best way to understand this is - Imagine that you have a dog, well that dog is an animal, but not all animals are dogs. In Java the way to assign an object as a sub-variable of another object is polimorphism.
the way to do that is to use the extends keyword it is used like this:
public class Cats extends Animals()
// all of the public methods from Animals will be usable in the Cats class.

1 comment: