Sei sulla pagina 1di 6

SCJP NOTES

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.

The names of the methods,variables or classes are called identifiers.

Keywords cannot be used as identifiers.

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.

Complete list of java keywords (alphabetical order)


abstract case final import strictfp this
assert const finally long super throw
boolean default goto native switch void
break do implements new static volatile
byte double int public synchronized while
char else if protected short
class enum float private throws
continue extends instanceof package transient
catch for interface return try

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.

A file can have more than one nonpublic class.

Files with no public classes can have a name that does not match any of the
classes in the file.

There are three access modifiers- public,protected, final.


there are four access controls (levels of access) – public,protected,final and default or package. All
four access controls work for most of the method and variable declarations but class can be declared
with only public or default access; the other two controls don't make sense for a class

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

class B extends A{}

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.

Abstract classes can't be instantiated, they can only be extended.


e.g.

Abstract class Car{


private double price;
private String model;
private String year;
public abstract void goFast();
public abstract void goUpHill();
..
..
}
Non-abstract methods can be put in an abstract class.Even if there's a single method that is abstract,
the whole class needs to be marked abstract.
Implementing a non-abstract method in an abstract class gives the concrete(non-abstract) subclass
inherited method implementations and now they only need to implement methods that define
subclass-specific behavior.
A class cannot be marked as both abstract and final. They have opposite meanings, a final class
must not be subclassed whereas an abstract class must be subclassed.

Differences in abstract class and an interface

Abstract class Interface


1. not all methods and variables need to be All methods need to be abstract.
abstract
2. Methods and variables need to be declared as Interface methods are implicitly public and
public and abstract. abstract.
3. Variables in an abstract class need not All variables defined in an interface must be
necessarily be static and final. They can be public, static and final. That is, interfaces can
concrete or abstract. declare only constants, not instance variables.
The variables declared in an interface are
implicitly 'public, static and final'.
Interface methods need not be static.
Because interface methods are abstract they
cannot be marked final, strictfp and native.
? An interface can extend one or more other
interfaces.
An interface can extend only another interfaces.
Cannot implement another interface or class.
Must be declared with a keyword interface.
Interface types can be used polymorphically.
The following method declarations won't compile

final void bounce(); // final and abstract can never be used


// together, and abstract is implied
static void bounce(); // interfaces define instance methods
private void bounce(); // interface methods are always public
protected void bounce(); // (same as above)

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 private method cannot be overridden by a subclass.

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,

default modifier = package


protected modifier = package+kids (through inheritance only)
Once the subclass inherits the protected member which is in another package, it becomes private
code to any class within the package of the child class, unless the class trying to access this 'private'
code is a subclass of the child class.
final is the only modifier that can be applied to a local variable.

Visibilty Public Protected Default Private


From the same Yes Yes Yes Yes
class
From any class in Yes Yes Yes No
the same package
From a subclass in Yes Yes Yes No
the same package
From a subclass Yes Yes,through No No
outside the same inheritance
package
From any non- Yes No No No
subclass class
outside the
package

Non access modifiers (final,abstract,synchronised,native,strictfp and static)

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(?)

Type Bits Min range Max range


Byte 8 (1 byte) -2^7 (2^7)-1
Short 16 (2 bytes) -2^15 (2^15)-1
Int 32 (4 bytes) -2^31 (2^31) - 1
Long 64 (8 bytes) -2^63 (2^63) -1
Float 32 (4 bytes) n/a n/a
Double 64 (8 bytes) n/a n/a

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.

public void addRewardpoints(List<Customer> customerIds){}

JEE
-----

Q. For a login page, write a servlet that makes the


a) the currently user available to all the pages in the web application
b) keeps all the currently logged in users available to all the pages in jsp.

SQL
------

Q. query to list out the names of all the co actors of Leonardo di caprio

Table1: A - Movie_id, movie_name, director_name


Table2: B - s.no.,movie_id,actor_name

Potrebbero piacerti anche