Showing posts with label Free Course. Show all posts
Showing posts with label Free Course. Show all posts
Java Access Modifiers

Java Access Modifiers

Java Access Modifiers
Java Access Modifiers

Java Access Modifiers

In this tutorial, I’ll describe how Java access modifiers work in detail. They manage the visibility and their members (methods and data). There are three Java access modifiers public, private, and protected. There is another modifier namely default which is implemented by default.
Java Access Modifiers Explained With Examples
Access Modifier
within class
within package
outside package by subclass only
outside package
Outside Class
Public
Y
Y
Y
Y
Y
Protected
Y
Y
Y
Y
N
Private
Y
N
N
N
N
Default
Y
Y
Y
N
N

Public Access Modifier

The accessibility of this modifier visible everywhere. We can say that public modifier doesn’t put any restriction on the access. This indicate that any public class, method, variable, interface, or constructor is accessible throughout the package. But if it belongs to another package then we need to import it. It has a broad scope among all other modifiers. The public access modifier can be useful to classes and interfaces along with methods, data members and variables.
Example of public modifier
public class FirstClass
{
    public void display()
    {
        System.out.println("I am Public Modifier");
    }
}
public class SecondClass
{
   public static void main()
   {
       FirstClass ob = new FirstClass();
       ob.display();
    }
       
}

Output:

I am Public Modifier
Here, in SecondClass, we can instantiate the FirstClass because its access modifier is public. The variables and methods inside the SecondClass class are also public. Hence, you are able to use it directly in your L SecondClass.

Private Access Modifier
This modifier can access Data members, methods and constructors within the class.             We cannot develop subclasses to that class which has only private constructors. Classes or interface cannot be declared as private because private modifier will make the class useless.
public class PrivateExampleBase
{
   private void display()
    {
        System.out.println("I am Private Modifier");
    }
}
In PrivateExampleBase display() method declare as private and when we called display() method from PrivateExampleDrive then it will generate error “display() has private access in PrivateExampleBase”
public class PrivateExampleDrive
{
   public static void main()
   {
       PrivateExampleBase ob = new PrivateExampleBase();
       ob.display();
    }
}
Another Example: This program is used to show that private members of a class can be accessed in the class only

class StudentClass {
    //private members of the class

    private int rollNo; // define as a private modifier

    private void show() {
        rollNo = 10;
       
        System.out.println("RollNo = " + rollNo);
    }
}

public class PrivateDataMember {
    public static void main(String args[]) {
        //creating StudentClass object
        StudentClass ob = new StudentClass();
        System.out.println(ob.rollNo);
        ob.show();
    }
}
In PrivateDataMember class we will try ti access private data member(rollNo) and also try to access show() method. But it will not be accessecible as it is define as private. And it'll display error as "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - rollNo has private access in MyFirstApp.StudentClass
            at MyFirstApp.PrivateDataMember.main(PrivateDataMember.java:15)"
Note: Private constructor cannot able to create an instance in another class.

Protected Access Modifier

The protected access modifier methods or data members are accessible within the same package or subclasses in a different package. We cannot declared protected in the classes because it used in the parent-child relationship. But the protected access modifier can be used on the data member, method and constructor.

package MyFirstApp;

class StudentClass {
    //protected members of the class
    protected int rollNo;
    protected void show()
    {
        rollNo = 10;
        System.out.println("RollNo = " + rollNo);
    }
}
public class ProtectedDataMember {
    public static void main(String args[]) {
        //creating Student class object
        StudentClass ob = new StudentClass();
        ob.show();
    }
}

Default Access Modifier

If we don’t mention any access modifier for a class, method or data member then we can say it is a default modifier. It can be accessed into all classes within the same package only. The default access modifier is also sometimes known as the package access modifier.

import java.util.Scanner;
public class Palindrom {
    int n, rev, m, a; // default access modifier
    void input() // this method contain no modifier
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number:");
        n = sc.nextInt();
        m = n;
    }

    void palin() // this method contain no modifier
    {
        rev = 0;
        while (m != 0) {
            a = m % 10;
            rev = rev * 10 + a;
            m /= 10;
        }
        if (rev == n) {
            System.out.println("Palindrom");
        } else {
            System.out.println("Not Palindrom");
        }

    }

    public static void main(String[] args) {
        Palindrom ob = new Palindrom();
        ob.input();
        ob.palin();
    }
}

More Tutorials

Linked List in Java

ArrayList in Java

Basic Features of java


8 Java Tutorial free resources

8 Java Tutorial free resources


Java Tutorials

Java Tutorials


Java is a computer programming language. It is a general-purpose, object-oriented, portable, secure and platform-independent language. It is intended to let application developers "write once, run anywhere" (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.  It was initiated in 1991 by James Gosling, Mike Sheridan, and Patric Naughton. Java was initially called Oak after an Oak tree. Later it named as Java.
There are 8 free resources given below. I have given the link, please follow the like. Happy Learning.


One of my favorite destinations of learning Java is Udemy.com, it is one of the largest and biggest online learning platforms in the world. There are several courses on this portal paid and free. It has many Java courses paid and free.
Practice Java by Building Projects

Practice Java by Building Projects

If your goal is to GET HIRED, then you need to PASS THE INTERVIEW!
The BEST way to prepare for a technical interview is to get practice building applications and applying core principles of object-oriented design. I've designed this course to accomplish that objective.


Many people fail interviews because:
·         They don't grasp object-oriented programming concepts
·         They can't apply object-oriented design in an actual program
·         They haven't practiced developing their applications by hand
·         They haven't debugged their code and overcome challenges
This course is designed for someone who simply wants to get better at understanding and applying Java design and object-oriented programming. It is perhaps the most effective thing you can do to prepare for your job interview. Forget about flashcards, forget about quizzes. Start practicing!
In this Practical Java course, you will build realistic projects from scratch. We will approach each program analytically and outline our approach to implement the solution. Then, piece by piece we will write the code to create a fully functioning application.
·         Understand client scenarios and requirements
·         Approach each problem analytically
·         Write our solution outline
·         Implement code step-by-step
·         Apply core principals of object-oriented design
Everyone hoping to start out in Java needs to practice! It's not enough to learn and watch, but you must try yourself! To enroll in this course and get typing!
Note: this course was built for someone who has some working knowledge and familiarity with Java or application design. Please learn some bit of Java programming and its concepts then you can build these projects. Good Luck.
Who is the target audience?
Current students or recent graduates
 Anyone looking to practice Java
Someone preparing for a Java interview
Someone starting a new Java project
 Anyone wanting a refresher on object-oriented programming or application architecture
Requirements
Familiar with Java development environment
Knowledge of object-oriented programming concepts recommended
Register in www.udemy.com and then click on Get the Code below...... enjoy the Java project and advance in your career. Good Luck.


 Get the Code