Difference Between Protected Or Default
Mar 28, 2015I am new in java. Is there any difference between protected or default when we are talking about one package?
View RepliesI am new in java. Is there any difference between protected or default when we are talking about one package?
View RepliesI was just wondering.. my understanding from everywhere I have read is interface cannot be private or protected (not at the top level) but when we declare an interface without any modifier it is default.
We know default modifier has more restricted access than protected.. public > protected > default > private
Now since an interface can be public and default then why not protected as clearly if they were allowed to be protected they could be implemented by a subclass..?
While typing this question I figured how would an interface know which is it's subclass? that is why Java allows only public i.e. any class can implement it or default i.e. any class within the package can implement.. am I right?
Why is it that a protected method cannot be accessed polymorphically outside its package. I suspect the reason is that some compile-time checks have to be performed. Is this correct?
View Replies View RelatedI don't know why class Object have two PROTECTED method -- clone() and finalize(). And in JUnit's source code, I notice that kent write :public class AClass extends Object {}I really don't understand what diffrient frompublic class AClass {}
View Replies View Relatedhow access levels work from subclasses and other packages, and I have discovered that a class cannot see it's own protected variable from another package which I thought it would be. I know that it says in the java docs "The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package." but I thought that would also include it's own class.
Java Code:
package food;
import food.fruit.*;
public class Food {
protected int protecte = 5;
private int privat = 5;
protected void method(){
[Code] ....
What is the real purpose of using Protected access specifier in Java?
View Replies View RelatedI have also tried above code with few changes but I got error. Code and error show below.
Java Code:
package practice;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
[Code] .....
Error:
Java Code:
Jun 16, 2014 6:21:33 PM practice.TestFTPProgram main
SEVERE: null
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
[Code] .....
With Apache POI it is possible to password protect an excel file. PFB for the code. But it is working with XLSX extension [2010 Excel format], But not with XLS [2003 Excel format].how to accomplish the same
package com.excel.utility;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
[code]..
How to read Password protected Excel file using java. Actually i read normal excel file(without password protection) successfully, using some standard codings with support POI jars. But i am unable to read password protected xls file.
View Replies View RelatedI found the following inheritance and encapsulation issue . Suppose you have a parent class with a non-static protected attribute.
package package1;
public class Parent{
protected int a = 10; // this is the non-static protected attribute in question
public static void main(String args[]){
// whatever logic
}// end Parent class
}// end main()
Now suppose you have a child class in another package and you have imported in the parent class.
package package2;
import package1.Parent;
public class Child extends Parent{
public static void main(String[] args){
Parent p = new Parent();
Child c = new Child();
System.out.println(p.a); //should print out 10 BUT DOES NOT
System.out.println(c.a); //should print out 10
}// end main()
}// end Child class
My observation is that p.a produces an error even though, to the best of my knowledge, it should not. I believe the statement "System.out.println(p.a);" should print out a 10.
Am I misunderstanding something about inheritance and encapsulation?
I am trying to declare fields as protected String custom.field.1096; in my java class but it does not allow me. Can I not declare the field as above? Is there any workaround to achieve this?
View Replies View RelatedI have a problem with several java applications. When I start them Java wants to connect to the certificate authority, to check if the certificate is still valid and not on a blacklist.
The problem is: my whole internet is behind a password protected proxy. If I open my browser i get a windows with username and password. I enter it and internet in the browser works. But for Java it isn't working, because I see no point, where I can enter the password and username for the proxy. I can enter the proxy ip and port in the java settings, but not the password and username. So I get a error screen from java, telling me, that java could not connect. I can disable the check in the java settings, but I don't wont that.
Is there a way to tell java, that java uses my proxy with my password and username? I already googled this problem and found nothing except tutorials for connecting with proxy in the java code. But these applications are not from me, I can't change the code ...
The LocalStudent class inherits the Student class. The IDE states an error of "no default constructor in Student class".I understand that if a LocalStudent object is created, the default constructor of its superclass (aka Student class) will be called implicitly.there is no LocalStudent object being created, so why is the default constructor of Student being called ?
The default constructor of LocalStudent is also overloaded by the created no-arg constructor containining subjects = null; . So there is no call to the superclass default constructor from the default constructor of LocalStudent.
public class Student {
private char year1;
public Student(String name, char year){
year1 = year;
}
public char getYear(){
return year1;
[code]...
Will I'm tying in my code to set a default number for the JTextField that when the user decide not to put any numbers. Like let say that I want the textfield set to 0 , so then the user do not file it it won't make any problem to the program because its already has a default number.
if (stringGQ != null && stringGW != null && stringGP != null){
stringGQ = gMQ.getText();
stringGW = gMW.getText();
stringGP = gMP.getText();
weightPrice_1M = Double.parseDouble(stringGW) * Double.parseDouble(stringGP);
[Code] .....
Note: This is a small part of my code. when I leave it empty it take the 0 as a value, However, when I write in text field it also take the value of a 0 and the finalTotal is also = to 0.what I'm doing wrong.
what is default literal for Boolean data type?
View Replies View RelatedArrayList l = new ArrayList();
System.out.println(l.size());
When I running the above code in eclipse the console show me the result as "0". So how to rectify this code to find default size as 10.
interface I1{
void show();
void display();
default void put(){
System.out.println("I am from interface I1");
[Code] ....
This code is not working..
class Course
{
String courseName;
}
class Entry
{
public static void main(String[] args)
{
Course c = new Course();
c.courseName="java";
System.out.println(c.courseName);
}
}
I have defined these two classes under same java project in Eclipse IDE with no package. Above two class are having default classes. class Course is also having a instance variable courseName with default access. So when we try to compile the Entry class it will give the errors while accessing the instance variable courseName on Line 6 and 7. As both the classes are having default access modifier. class Course is not visible to class Entry, then why we do not get any compilation error while creating the object of class Course on line 5?
I have a question related to q44 of Examlab.
public static void main(String... args) {
String i;
// String i = null; that would compile
if (i == null) {// this line does not compile as i was not initialized
System.out.print("A");
} else {
System.out.print("B");
i = "A";
main("A", "B");
}
}
Why does the above code not compile although the statement String i should lead to an initialisation of i to the value of null which is the default value for Strings.
I remember reading Instance variables & local variables (in methods) are initialized to appropriate value by the compiler.
Is this true for static class variables and variables in nested classes ?
I want to programmatically change the default program which an extension is opened in. So .resantic extensions for example to run in my jar file when I double-click on it. I know i need a bat file or exe file because i can't directly run the jar file. But i was wondering if its possible to run it if its an executable jar file. Else i can just make a bat file that runs my program. Also how can i on double click send the path to the file that was clicked in the args[] array so i can open the actual file aswell instead of just opening the program?
View Replies View RelatedIn the following code the print method prints the default value of int(zero) for the first time even when the variable i has been assigned a value of 4. Why?
class A1{
A1()
{
System.out.println("Inside constructor of A1()");
print();
}
void print()
{
System.out.println("A");
[Code] ....
Output:
Inside constructor of A1()
0
Inside constructor of B1()
4
I am facing difficulties in identifying the proper way to add the selected colors of cards into an arraylist. I am having an arraylist which is:
Java Code: private List<String> selectedCars = new ArrayList<String>(); mh_sh_highlight_all('java'); And one more for the carColors:
Java Code: private List<String> carColors = new ArrayList<String>(); mh_sh_highlight_all('java');
The selectedCards array will store the selected cars ['TOYOTA','MAZDA','NISSAN'] as per the selection from the user.For certain types, there is one default color which is 'Black', however for some of them, the user can select different colors.(ex. if selection is 'Toyota')
Java Code:
String carColor="";
String toyotaColor=""; // The value will be retrieved from the form once the user selected the color
if (selectedCars.contains("MAZDA"))
{
carColor="Black";
}
else if (selectedCars.contains("TOYOTA"))
{
carColor=toyotaColor;
[code]....
I am writing a program that accepts input from the user, I want default values displayed before the input values.
Java Code:
public surfboards() {
surfboardType = "Type not assigned";
shaperName = "Shaper not assigned";
constructionType = "Construction Type not assigned";
surboardSize = 0D;
} mh_sh_highlight_all('java');
I can get the output to display as shown below
(THIS IS WHERE I WANT DEFAULT CONSTRUCTOR DISPLAYED. Currently not displayed)
Type not assigned
Shaper not assigned
Construction Type not assigned
0 Feet
(THIS IS WHERE I WANT DEFAULT CONSTRUCTOR DISPLAYED. Currently not displayed)
input:
Java Code:
Enter Surboard Type:
test
Enter Shaper Name:
test
Enter Surfboard Construction Medium:
test
Enter Surfboard Size:
6.5 mh_sh_highlight_all('java');
Output:
test
test
test
6.5 feet
I want to use my implemented toolkit while loading applets. So, I have set the Runtime Parameters
-Dawt.toolkit=org.sug.MyToolkit in Java Control panel.
But when it is not picking MyToolkit, instead its default toolkit sun.awt.windows.WToolkit.
I am able to set it perfectly in Java 6 but it is not working in java 7.
I just recently started learning about encapsulation, how to set variables private and only accessible to other classes through setters() and getters(). I've seen a couple of programming examples, perhaps not enough but some from here, and I sort of built the assumption that by default all variables need to be private. to make things more clear, here's a card dealer I made which simply
1- generates a fulll deck of 52 cards
2- lets user decide how many players, with 5 as Max number allowed as each player is dealt 10 cards.
3- deal cards
I approached this by making A deck , card , player and game class
import java.util.ArrayList;
public class Deck {
//an Object of this Class would generate a full deck ie an ArrayList of 52 Cards
private String[] suits={"Spades","Diamond","Clubs","Hearts"};
private int[] number={1,2,3,4,5,6,7,8,9,10,11,12,13};
ArrayList<Cards> deck= new ArrayList<Cards>();
[code]....
I can understand why for example Deck class's suit and number arrays are set to private , as they only need to be accessed by this class only. however, both the Deck class's deck arraylist and the Player class arraylist are not private, as I need to transfer these values from one to the other and it felt to me that setting them to private would make it more difficult to deal with them, but what I did instead is to set the Game class dealCard(), which is the only method that have access to them as private. does this achieve the same effect or do I need to set both of these arrayList to private?a follow up question, this is more related to the actual card dealer program, in this code
private void dealCards(){
for(int x = 0 ; x < playerCount ; x++){
for(int y = 0 ; y < 10; y++){
playerList.get(x).pile.add(deck.deck.get(0));
deck.deck.remove(0);
}
}
}
is there an API in ArrayList class that moves(adds to receiver and remove from giver) element across ArrayLists?