Ticker

6/recent/ticker-posts

Header Ads Widget

Responsive Advertisement

Override And Access Modifier With Java

Override And Access Modifier With Java

  • Overriding

Overriding is a subclass method the same as a super class method, the parameters are the same but the statement or implementation is different. Or overriding can be interpreted as a condition where the child class can change or we can say modify or expand data and methods in the parent class.
The advantage of Overriding is that it can add more properties to the parent class

Briefly Overloading:
1. Define a method with the same name in different classes.
2. The concept can be utilized in some inheritance cases, where it wants to name a method with the same name but different in its implementation.

For more details:
1. If the overridden (in the parent diclass) method is default then overriding (in the child diclass) must be default, protected or public.
2. If the overridden (in the parent diclass) method is protected then overriding (in the child diclass) must be protected or public.
3. If the overridden (in the parent diclass) method is public, then overriding (in the child class) must be public.

Overriding Rules
1. The overriding method access mode must be the same or broader than the overriding method.
2. Subclasses can only override the superclass method once, there cannot be more than one method in the same exact class.
3. The overriding method cannot throw checked exceptions that are not declared by the overridden method.
4. Method instances can be overridden only if they are inherited by sub classes.
5. A constructor cannot be overridden

Using Super Keywords
To use the superclass version of the overriding method, you can use the super keyword.

  • Modifier Access 

 Is what is given to a variable / method / class in order to maintain the integrity of the data when it wants to be accessed by other objects. The right of access itself is given by the program maker as one form of implementation of the OOP itself. With the Access Modifier, we can limit which resources can be accessed by certain objects, their derivatives, or by certain methods.

1. Public
For the scope of the public modifier can be used anywhere without restrictions. This reason is the answer to the question above. So the class must be given a public modifier so that it can be called in another class without restrictions. For ways to check can see the example above, the public can be called in another package. Then we create another sub package from packageSatu and name packageSatu. In the past, then create a class for FOUR then only the public can access it.
2. Private
The Private Modifier can only be called in the same class and cannot be called in another class. Private is used to encapsulate objects, for more details about encapsulation I will discuss later.
3. Protected
For the scope of the protected modifier the results I tried were the same as the default modifier added in the child class as well.
4. Default
For default the scope is in one package so it cannot be accessed in another package.

DOWNLOAD MODUL OVERRIDE AND ACCESS MODIFIER

LINK 1
LINK 2

DOWNLOAD EXAMPLE PROGRAM OVERRIDE AND ACCESS MODIFIER

LINK 1
LINK 2



Post a Comment

0 Comments