Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Class – A template that describes the kind of behavior and state that object of its type will support
State (instance variables) – The values assigned to an object's instance variables make up an object's
state.
Behavior(methods) - Methods have all he logic of the class. Methods are where the real work gets
done.
Inheritance allows code defined in one class to be reused in other classes. The superclass knows
nothing of the classes that inherit from it, but all of the subclasses that inherit from the superclass
must explicitly declare the inheritance relationship. A subclass that inherits from a superclass is
automatically given accessible instance variables and methods defined by the superclass, but is also
free to override superclass methods to define more specific behavior.
Car{
StartEngine()
StopEngine()
Accelerate()
Brake()
}
Ferrari{ BMW{
Luxurious seating() GreatLooks()
accelerate() Price()
} brake()
}
Interface is a 100 percent abstract class. It defines the methods that a sub class must support but not
how they must be supported.
Importing of classes allows a programmer to use objects from classes created by the programmer
and/or by others, like Sun's Java API classes.
Java organises classes into packages and uses import statements to give programmers a consistent
way of managing the naming and access to the classes they need.
Import and package statements apply to all classes within a source code file. In other words, there's
no way to declare multiple classes in a file and have them in different packages, or use different
imports.
Files with no public classes can have a name that does not match any of the
classes in the file.
Default Access – when no access modifier is specified for the class, it is a default access or
package-level access,because a class with default access can be seen only by classes within the
same package.
e.g
package pckg1
class A{}
package com.file2.pckg2;
import pckg1.A; // Compilation fails
Public Access-All classes have access to the class having the 'public' keyword in its declaration.
However if the public class that has to be accessed is in a different package, this class will have to
be imported before its properties can be used.
Other Class Modifiers, that is non-access, are : final, abstract and strictfp.
Access modifiers can be used in combination with the non-access modifiers but non-access
modifiers cannot always be mixed.
strictfp is used to modify class and methods only, not variables. Marking a class as strictfp means
that any method code in the class will conform to the IEEE 754 standard rules for floating points.
Without that modifier ,floating points used in the methods might behave in a platform-dependent
way. If you don't declare a class as strictfp, you can still get strictfp behavior on a method-by-
method basis, by declaring a method as strictfp.
Final classes can't be subclassed. A class is declared as final only when an absolute guarantee that
none of the methods in that class will ever be overrridden is required.
An interface variable is final (also public and static) and can never be given a value by the
implementing (or any other) class.
The reference this always refers to the currently executing object—in other words, the object
running the code where you see the this reference.
A default member (instance variables and methods) can be accessed only if the class accessing the
member belongs to the same package but a protected member can be accessed (through inheritance)
by the subclass even if the subclass is in a different package. In case of inheritance too, a subclass
cannot access a default member of its superclass if it is not in the same package.So,
Final methods : 'final' keyword prevents the method to be overridden in a subclass, and is used to
enforce the API functionality of a method. e.g. The following code will give error
class Superclass{
public final void showSample(){
system.output.println('something');
}
}
class Subclass{
public void showSample(){ // error since showSample() is declared as final in parent class
system.output.println('another thing');
}
}
Final arguments must keep the same value that the parameter had when it was passed into the
method.
Abstract methods are the ones that are declared as abstract and do not contain a body or their
implementation. Such methods do not have curly braces and are closed by semicolon. The first
concrete subclass of an abstract class must implement all abstract methods of the superclass. That
means if the subclass itself is an abstract class then it may not implement all the abstract methods of
the superclass.
We can have an abstract class with no abstract method but it is illegal to have even a single abstract
method in a class that is not explicitly declared absract. e.g below is illegal
public class IllegalClass{ //illegal, needs to be declared abstract
public abstract void myMethod();
public void notAbsMethod(){
..
}
}
Abstract methods must be implemented by a non-abstract subclass. If the subclass is abstract, it is
not required to implement the abstract methods, but it is allowed to implement any or all of the
superclass abstract methods.
A method can never be marked both abstract and final or both abstract and private. It cannot be
combined with static either.
Synchronized Methods : the synchronized keyword indicates that the method can be accessed by
only one thread at a time. This keyword can be applied only to methods, not to variables or classes.
Native Methods : The native modifier indicates that a method is implemented in a platform-
dependent code, often in C. It can be applied only to methods, not classes or variables. The native
method's body implementation must be a semicolon, like abstract methods indicating that the
implementation is omitted.
Strictfp methods: can be applied to classes and methods. strictfp forces floating points (and any
floating point operations) to adhere to IEEE754 standard. This helps in predicting how your floating
points will behave regardless of the underlying platform the JVM is running on.
Constructors can have all normal access modifiers. Constructors can't be marked static, abstract or
final.
Primitive variables are the ones which are declared with primitive data types. They can be declared
as class variables (having static modifier), instance variables, method parameters, or local variables.
For integer types the sequence from small to big is byte,short,int, long,float and double(?)
Boolean types do not have range; a boolean can either be true or false. It is virtual machine
dependent.
The char type is a single 16-bit unicode character.
Unicode characters are represented by unsigned 16-bit integers, which means 2^16 possible values.
Because char is an integer type, it can be assigned to any number type large enough to hold 65535
values. Although short and char both are 16-bit types, short uses 1 bit to represent sign,so fewer
positive numbers are acceptable in short.
Interview questions
core java
-----------
Q. to add 100 reward points to each of the customers in the table Customer whose customerId is
passed as a parameter to the method.
JEE
-----
SQL
------
Q. query to list out the names of all the co actors of Leonardo di caprio