#TC201 #Topic12 What is polymorphism?

Polymorphism in a cientific way is when we use inhertiance to create new classes, each new class inherits both the data members and the methods of the superclass. But simethimes the methods need to behave differently depending on the new class deinition. In the case where the subclasses may need to behave differently, they still contain the same method name but they have a different implementation.

Well, in few words and well explained is when a subclass inherit a method from the superclass and another subclass inherits the same method and both of them do something related to the method but not the same thing each of them override the generic method that the superclass inherits to do what they need to do.

Source: Polymorphism

#TC201 #Topic12 What is polymorphism?

Polymorphism in a cientific way is when we use inhertiance to create new classes, each new class inherits both the data members and the methods of the superclass. But simethimes the methods need to behave differently depending on the new class deinition. In the case where the subclasses may need to behave differently, they still contain the same method name but they have a different implementation.

Well, in few words and well explained is when a subclass inherit a method from the superclass and another subclass inherits the same method and both of them do something related to the method but not the same thing each of them override the generic method that the superclass inherits to do what they need to do.

Source: Polymorphism

#TC201 #Topic12 What is polymorphism?

Polymorphism in a cientific way is when we use inhertiance to create new classes, each new class inherits both the data members and the methods of the superclass. But simethimes the methods need to behave differently depending on the new class deinition. In the case where the subclasses may need to behave differently, they still contain the same method name but they have a different implementation.

Well, in few words and well explained is when a subclass inherit a method from the superclass and another subclass inherits the same method and both of them do something related to the method but not the same thing each of them override the generic method that the superclass inherits to do what they need to do.

Source: Polymorphism

#TC201 #Topic12 What is polymorphism?

Polymorphism in a cientific way is when we use inhertiance to create new classes, each new class inherits both the data members and the methods of the superclass. But simethimes the methods need to behave differently depending on the new class deinition. In the case where the subclasses may need to behave differently, they still contain the same method name but they have a different implementation.

Well, in few words and well explained is when a subclass inherit a method from the superclass and another subclass inherits the same method and both of them do something related to the method but not the same thing each of them override the generic method that the superclass inherits to do what they need to do.

Source: Polymorphism

#TC201 #Topic12 What is polymorphism?

Polymorphism in a cientific way is when we use inhertiance to create new classes, each new class inherits both the data members and the methods of the superclass. But simethimes the methods need to behave differently depending on the new class deinition. In the case where the subclasses may need to behave differently, they still contain the same method name but they have a different implementation.

Well, in few words and well explained is when a subclass inherit a method from the superclass and another subclass inherits the same method and both of them do something related to the method but not the same thing each of them override the generic method that the superclass inherits to do what they need to do.

Source: Polymorphism

#TC201 #Topic10 What are visibility modifiers, their purpose and use in practice?

--Originally published at Venkon Programming

Let's start with this chart: 

            | Class | Package | Subclass | World
————————————+———————+—————————+——————————+———————
public      |  y    |    y    |    y     |   y
————————————+———————+—————————+——————————+———————
protected   |  y    |    y    |    y     |   n
————————————+———————+—————————+——————————+———————
no modifier |  y    |    y    |    n     |   n
————————————+———————+—————————+——————————+———————
private     |  y    |    n    |    n     |   n

y: accessible
n: not accessible
  • private member is only accessible within the same class as it is declared.

  • A member with no access modifier is only accessible within classes in the same package.

  • protected member is accessible within all classes in the same package and within subclasses in other packages.

  • public member is accessible to all classes (unless it resides in a module that does not export the package it is declared in).

 

 

Which modifier to choose?

Access modifiers is a tool to help you to prevent accidentally breaking encapsulation(*). Ask yourself if you intend the member to be something that's internal to the class, package, class hierarchy or not internal at all, and choose access level accordingly.

Access Control and Inheritance:

The following rules for inherited methods are enforced:

  • Methods declared public in a superclass also must be public in all subclasses.

  • Methods declared protected in a superclass must either be protected or public in subclasses; they cannot be private.

  • Methods declared private are not inherited at all, so there is no rule for them.

 

Sources:

Modifiers

Access modifiers

#TC201 #Topic10 What are visibility modifiers, their purpose and use in practice?

Let’s start with this chart: 

            | Class | Package | Subclass | World
————————————+———————+—————————+——————————+———————
public      |  y    |    y    |    y     |   y
————————————+———————+—————————+——————————+———————
protected   |  y    |    y    |    y     |   n
————————————+———————+—————————+——————————+———————
no modifier |  y    |    y    |    n     |   n
————————————+———————+—————————+——————————+———————
private     |  y    |    n    |    n     |   n

y: accessible
n: not accessible
  • private member is only accessible within the same class as it is declared.

  • A member with no access modifier is only accessible within classes in the same package.

  • protected member is accessible within all classes in the same package and within subclasses in other packages.

  • public member is accessible to all classes (unless it resides in a module that does not export the package it is declared in).

 

 

Which modifier to choose?

Access modifiers is a tool to help you to prevent accidentally breaking encapsulation(*). Ask yourself if you intend the member to be something that’s internal to the class, package, class hierarchy or not internal at all, and choose access level accordingly.

Access Control and Inheritance:

The following rules for inherited methods are enforced:

  • Methods declared public in a superclass also must be public in all subclasses.

  • Methods declared protected in a superclass must either be protected or public in subclasses; they cannot be private.

  • Methods declared private are not inherited at all, so there is no rule for them.

 

Sources:

Modifiers

Access modifiers

#TC201 #Topic3 What is encapsulation?

--Originally published at Venkon Programming

 

Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class, therefoe it is also known as data hiding.

To achieve encapsulation in Java

  • Declare the variable of a class as private
  • Provide public setter and getter methods to modify and view the variables values.

Encapsulation is used to reduce dependencies, so if you want to change something you can change it and it won't affect other classes because it was encapsulated and it only affects that class.

Example:

#TC201 #Topic3 What is encapsulation?

Source: Encapsulation