Question

Write down a simple java program that demonstrates the properties of different access modifiers (private, public,...

Write down a simple java program that demonstrates the properties of different access modifiers (private, public, and protected) in different classes.

Homework Answers

Answer #1


import java.util.*;

class A{
private int a;//this variable only accessed by the methods inside the object A //it can't be accessed outside of class A
A(int b)//constructor
{
a=b;
}
public void printA()//it can accessed outside of the classA
{
//since printA is the method in classA , it can access varaible 'a'
System.out.println("A:"+a);//printing A value
B b = new B(10);
System.out.println("From A:");//printing B value
b.printB();
}
  
}
class B
{
protected int b;//it only accessed by the members of classB,other classes which are in the same package with class B and by subclasses
B(int c)//constructor
{
b=c;
}
public void printB()//it can accessed outside of the classA
{
//since printB is the method in classB , it can access varaible 'b'
System.out.println("B:"+b);//printing B value
}
}
class C extends B//inherits properties of B
{
C()
{
super(2);
}
//here C is subclass of B
public void printC()
{
System.out.println("From C, B:"+b);//printing B value
  
}
  
}
public class Main
{
   public static void main(String[] args) {
   //testing all classes
A a = new A(1);
a.printA();
B b = new B(2);
b.printB();
C c = new C();
c.printC();
   }
}

output:

run:
A:1
From A:
B:10
B:2
From C, B:2
BUILD SUCCESSFUL (total time: 1 second)


//PLS give a thumbs up if you find this helpful, it helps me alot, thanks.


//if you have any doubts, ask me in the comments

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Given the following package contents, write a short program (class Insecttest) that demonstrates the concept of...
Given the following package contents, write a short program (class Insecttest) that demonstrates the concept of polymorphism using the classes provided in JAVA package insect; public class Insect {      public void nickname () {        System.out.println ("bug");      }      } public class Flying extends Insect {      @Override      public void nickname() {           System.out.println ("flying bug");      }      } public class Jumping extends Insect{           @Override           public void nickname() {                System.out.println ("jumping bug");           }           }
Need in Java coding Write a program that will allow users to enter different types of...
Need in Java coding Write a program that will allow users to enter different types of books into the program, and at the end print out everything that the user entered. Allow the user to enter as many books as they want. Please collect the following information and break down the classes as follows using inheritance/classes. I have added the extra properties that each class should include. Book types: Paper Book (nothing extra) Comic Book (issue number) Magazine (publisher, issue...
Write program in C#. WAP on Encapsulation to hide the type members with private access and...
Write program in C#. WAP on Encapsulation to hide the type members with private access and display employee details. • Create a new project, select console application from the template and name the project as “EmployeeDetails.cs”. • Here type members are nothing but properties. • Create another class as Employee and write the properties for EmpId, EmpName and Salary. • Now, create an instance of the Employee class in the “EmployeeDetails” class and assign the values for EmpId,EmpName and Salary...
Write a java program to implement the RSA public-key cryptosystem.
Write a java program to implement the RSA public-key cryptosystem.
Write a pseudocode for the following java programs: public class Item {    private String name;...
Write a pseudocode for the following java programs: public class Item {    private String name;    private double cost;    public Item(String name, double cost) {        this.name = name;        this.cost = cost;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public double getCost() {        return cost;    }    public void setCost(double cost) {...
Write Java program Lab51.java which takes in a string from the user, converts it to an...
Write Java program Lab51.java which takes in a string from the user, converts it to an array of characters (char[] word) and calls the method: public static int countVowels(char[]) which returns the number of vowels in word. (You have to write countVowels(char[]) ).
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Write a Java program with a method public String replaceChar(String p, int k, char c) {...
Write a Java program with a method public String replaceChar(String p, int k, char c) { } that given a String p, an int k, and a char c, returns a String with the kth character replaced by c. Of course, 0<=k<=p.length()-1, otherwise raise an exception or error.
Java programming. Write a public Java class called WriteToFile that opens a file called words.dat which...
Java programming. Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”}
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT