Q&A
Q&A
Q&A
Abstraction
Polymorphism
Inheritance
Encapsulation
Q: What is Polymorphism?
A: Polymorphism is briefly described as "one interface, many implementations."
Polymorphism is a characteristic of being able to assign a different meaning or
usage to something in different contexts - specifically, to allow an entity such as a
variable, a function, or an object to have more than one form.
Q: How does Java implement polymorphism?
A: (Inheritance, Overloading and Overriding are used to achieve Polymorphism in
java). Polymorphism manifests itself in Java in the form of multiple methods
having the same name.
In some cases, multiple methods have the same name, but different formal
argument lists (overloaded methods).
In other cases, multiple methods have the same name, same return type,
and same formal argument list (overridden methods).
Method overloading
Method overriding through inheritance
Method overriding through the Java interface
The overriding method cannot have a more restrictive access modifier than
the method being overridden (Ex: You can�t override a method marked
public and make it protected).
You cannot override a method marked final
You cannot override a method marked static
Q: What is super?
A: super is a keyword which is used to access the method or member variables from
the superclass. If a method hides one of the member variables in its superclass,
the method can refer to the hidden variable through the use of the super keyword.
In the same way, if a method overrides one of the methods in its superclass, the
method can invoke the overridden method through the use of the super keyword.
Note:
Q: What is Constructor?
A: A constructor is a special method whose task is to initialize the object of its
class.
It is special because its name is the same as the class name.
They do not have return types, not even void and therefore they cannot
return values.
They cannot be inherited, though a derived class can call the base class
constructor.
Constructor is invoked whenever an object of its associated class is
created.
where, the name of the variable is varIdentifier and its data type is specified by
type.
Note: Static variables that are not explicitly initialized in the code are automatically
initialized with a default value. The default value depends on the data type of the
variables.
Q: What is static block?
A: Static block which exactly executed exactly once when the class is first loaded into
JVM. Before going to the main method the static block will execute.
Q: What is the difference between static and non-static variables?
A: A static variable is associated with the class as a whole rather than with specific
instances of a class. Non-static variables take on unique values with each object
instance.
Q: What are static methods?
A: A static method can only call other static methods.
A static method must only access static data.
A static method cannot reference to the current object using
keywords super or this.
Q: What is an Interface?
A: An interface is a description of a set of methods that conforming implementing
classes must have.
Note:
If various implementations are of the same kind and use common behavior
or status then abstract class is better to use.
When you want to provide a generalized form of abstraction and leave the
implementation task with the inheriting subclass.
Abstract classes are an excellent way to create planned inheritance
hierarchies. They're also a good choice for nonleaf classes in class
hierarchies.