Showing posts with label javaquestionswise. Show all posts

Learn Java Tutorials Questionwise,Java Tutorials- Questions And Answers -3-Interface

No comments
Here is another article explaining the core java topics in the questions answers form.In this article we have provided some basic questions ans answers about interfaces in java, we will have a broad look into this topic in our upcoming series of question answers. We have covered almost all the basic concepts about interfaces in java with the help of different questions,what is an interface,how to declare an interface in java? and much more.But still if you have some doubts regarding the topic explained in the article then feel free to comment your queries in the comment box.

Q.1 What is an interface in java?

Answer- In java an interface is an 100% abstract class having only abstract methods and constants.When we define an interface we define a contract for what a class can do ,but how class will do it ,it totally depends upon the class. An interface can be implemented by any class from any inheritance tree and only an interface can extend another interface.
Whether we define it or not while declaring an interface in java ,but an interface is always abstract.

Q.2 What is the difference between an interface and abstract class in java?

Answer-

Interface Abstract Class
Only abstract methods are allowed in an Interface Both abstract and non abstract methods are allowed in an abstract class
Interface can extend another interface. A class can never extends an interface
An interface is implemented by a class An abstract class is extended by another class.
An interface can have only static and final variables An abstract class has no such restriction
No need to use abstract keyword while declaring an interface or any of its methods We always need to use abstract keyword while declaring an abstract class and for methods if methods are abstract.

Q.3 Can we declare variables inside Interface if yes then what kind of variables are allowed within an interface?

Answer-we can declare only static final variables inside an interface ie constants.All the classes implementing the interface will have the same value of the variables declared inside an interface.

Q.4 How to declare an interface?

Answer-

We can define an interface using interface keyword with its name like-
interface Bouncable{
//abstract methods
}
OR

public abstract interface Bouncable{
//abstract methods
}



We can or can’t use abstract keyword while declaring an interface because an interface is always public abstract by default. So both the above declarations of interface are correct.

Q.5 Is it necessary to use abstract keyword while declaring an interface? Is it illegal to use abstract keyword while declaring an interface? Why?

Answer-No, it is not necessary to use abstract keyword while declaring an interface in java because as described in the previous answer an interface is always public and abstract by default. But it is always legal to use abstract while declaring an interface.

Q.6 What if we don’t define any modifiers before declaring a method or variable inside an interface?

Answer- If we don’t use any modifiers while declaring an interface method or variable then by default-
An interface method is always public abstract.
An interface variable is always final static.


Learn Java Tutorials Questionwise,Java Tutorials- Questions And Answers -2

No comments
This is the second article in the series of learning java in the form of question answers form.In this article we have explained some more basic but important concepts of core java in questions -answers form.We hope you will like it.

Q.1 How many classes in one source code file we can declare and how many of them we can declare as public?

Answer-We can declare any number of non-public classes in a single source code file in java, but there should be only one public class in the source-code file.

Q.2 What should be the name of the source code file in java?

Answer-If we have more than one class declared in a source code file then, the name of the file should be as same as the name of the public class in the source code file.

Q.3 What should be the location of the import statements, package names and comments in a source code file in java?

Answer-In a java source code file , the very line should be the package declaration, and after then all the import declaration statements should be written if there are any and after that we should declare the class.

Q.4 How to declare a class in java?

Answer-Syntex for declaring a class in java-


 class  {
//code inside the class
}

For example-
public class Apple{

}


In the above code a class is declared with name Apple and access modifier public.

Q.5 What are the type of modifiers in java?

Answer-There are two type of modifiers in java-

  • Access modifiers(public,private,protected)

  • Non access modifiers(final,static,abstract,stricfp..)

Q.6 What is the meaning of class access or accessing a class?

Answer-If we say that we can access a class that means we can do the following things with that class-

  • we can create an instance of that class from any other class which has access to that class.

  • we can extend that class

  • We can override its methods depending on the access modifiers of the methods.

Q.7 Explain default access modifier for a class in java?

Answer-if we do not declare any access modifier before a class then it comes under default access, which is a package level access modifier,that means the class is accessible only to the classes within the same package.

Q.8 What is the meaning of import statements in java?

Answer-import statements are used in class for importing the classes from a different package so that they can be referred in the class with just class name and there is no need to write the class name with the full package name to refer it.

Q.9 How to import all the classes from a package in java?

Answer-To import all the classes from a package we use * at the end of the package name.
For example- java.util.*
It means all the classes inside the util package has been imported.

Q.10 Can we refer to a classes from another package without using import statement?

Answer-yes we can refer a class from another package in java without using import statements, we just need to write the class name with full package name.

Q.11 What is a public class in java?

Answer-A public class can be accessed by all the classes in the java universe without any restriction,from any package.

Q.12 What are the access and non access modifiers we can use for a class in java?

Answer-access modifiers used for classes in java are- public,default
non-access modifiers used for classes in java are-final,abstract,strictfp

Q.13 Can we declare a class with using modifiers final and abstract together? why?

Answer-no we can never use a class with using both modifiers final and abstract together,because they are used to define totally opposite meaning, An abstract class can never be instantiated so it must be extended in order touse it while a final class can never be extended by any other class.

Q.14 What is a final class in java?

Answer-A final class in java can never be subclassed ie no class can extend a final class. We should only declare a class final only if we are sure that we need no specific subclass for that class.

Q.15 What is an abstract class in java?

Answer-An abstract class in java can never be instantiated ie we can not create objects of instances of an abstract class, the only way to use an abstract class is to extend it.

Q.16 Can we declare an abstract method in a non abstract class?

Answer-No we cannot declare any abstract method inside a non-abstract class,if a class has even a single abstract method then the whole class need to be declared as abstract.

Q.17 Can we declare an non-abstract method in an abstract class?

Answer-We can declare any number of non-abstract methods inside a abstract class.


This was another article in which we have explained some more basic concepts of java programming in the question answer form.I hope you enjoyed learning it in question-answer form.If you have any questions regarding the topics explained above then feel free to ask in the comments.
thanks..

Learn Java Tutorials Questionwise,Java Tutorials- Questions And Answers -1

No comments
Here is an attempt to provide the concepts of java programming language in question answers form. This is the first article on this topic where the basic java concepts are provide in simple question answers form which will help you in understanding and remembering the concepts more quickly. This question answer approach is also very helpful for interviews where we face various concepts in questions form.

Q.1 What is a java program?

Answer- A java program is a collection of objects which talk to each other using their methods

Q.2 What is a class in java?

Answer- In java a class is a template which has methods and instance variables which defines a class.

Q.3 What is object in java?

Answer- In java an object is an instance of the class in the java, each object of a class type has its own set instance variables.

Q.4 What is the meaning of state and behaviour of an object in java?

Answer- In java each object has its own set of instance variables defined its class, instance variables in the objects with values defines the state of an object. Behaviour are the methods defined in class, all the logic and algorithms are written inside a method of a class, all the objects talk to each other with the help of their methods.

Q.5 What are identifiers in java?

Answer- like any other computer language in java we need to name the different classes, objects ,methods and variables ,these names are called the identifiers.

Q.6 What is Inheritance in java ? explain with a example in brief?

Answer- Inheritance is one of the OOP language property ,with the help of inheritance we can reuse the code written inside a class in another class.The class which extends another class is called subclass and the class which is being extended by some other class is called superclass. A superclass does not know anything about the subclass which has extended it , but a subclass has access to all the instance variables and methods defined inside a superclass except private ones.

Q.7 What is an Interface In java? Explain with a brief example?

Answer- In java an interface is 100% abstract class which has abstract methods.(abstract methods are the methods without body) To use an interface a class need to implement it by implementing all its methods. A class implement an interface using implements keyword.

Q.8 What are the rules for naming of legal identifiers in java?

Answer- There are some rules for naming of identifiers, they are-

  • An identifier should start with a letter, a currency symbol like $ or a conneting character like _(underscore).

  • An identifier should not start with a number

  • An identifier can have any combination of numbers,letters,currency characters or connecting characters after the first character.

  • An identifier can have any number of characters.

  • A java reserved keyword can never be used as an identifier.

Q.9 What are the naming standards for the naming of identifiers in java recommended by Sun?

Answer- Naming standards recommended by sum for various identifiers-

Identifiers for classes-

A class name should start with a capital letter, and after it should follow camelCase. Like- Car ,Animal, SubmitButton are all legal class identifiers. It is good to have class name which is a noun.

Identifiers for methods-

A method name in java should start with a small case letter, and then simple camelCase. It is recommended to have a method name in java which is a combination of verb and noun, Like- getName()

Identifiers for variables-

A variable name should also follow camelCase, and should be short and meaningful.

Identifiers for constants-

In java constants are named with capital letters with _ as separators. A constant in java is a static and final keyword.

Q.10 What is a bean in java?

Answer- In java a bean is a class with properties which are private and these properties of the bean can only be accesses by the getter and setter methods of the class outside a class.

Q.11 What are getters and setters in java class?

Answer- getters and setters in java are the methods defined inside a class(bean) used for setting and getting the values of its properties which are private.



If you have any questions related to the concepts explained in above questions then feel free to comment below , and we will try to solve your queries.
Thanks for reading,
Next series of questions will be uploaded soon.