Skip to content

Commit b567871

Browse files
author
Ram swaroop
committed
added classes and interfaces notes
1 parent d5103ba commit b567871

2 files changed

Lines changed: 81 additions & 4 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,63 @@ layout: post
33
title: Declarations
44
---
55

6+
### Class
7+
8+
* There can be __only one public class__ per source code file.
9+
* If there is a public class in a file, the name of the file must match the name of the public class.
10+
* Files with no public classes can have a name that does not match any of the classes in the file.
11+
* A file can have __more than one nonpublic class__.
12+
13+
14+
### Interface
15+
16+
* All __interface methods are implicitly `public` and `abstract`__. In other words, you do not need to actually type
17+
the `public` or `abstract` modifiers in the method declaration, but the method is still always `public` and `abstract`.
18+
* All __variables defined in an interface are implicitly `public`, `static`, and `final`__, in other words, interfaces can
19+
declare only constants, not instance variables.
20+
* Interface methods must not be `static`, `final`, `strictfp`, or `native`.
21+
22+
#### Some valid interface declarations:
23+
24+
{% highlight java %}
25+
26+
public abstract interface Rollable { } // abstract is redundant as
27+
// interfaces are implicitly abstract
28+
29+
public interface Rollable { } // public modifier is required if you
30+
// want the interface to have public
31+
// rather than default access
32+
33+
interface Rollable { } // interface with default access
34+
35+
public interface Bounceable {
36+
37+
// following initializations/declarations are all
38+
// legal and identical
39+
40+
int b = 8;
41+
public static final int b = 8;
42+
public final int b = 8;
43+
static final int b = 8;
44+
final int b = 8;
45+
46+
void bounce();
47+
public void bounce();
48+
abstract void bounce();
49+
public abstract void bounce();
50+
abstract public void bounce();
51+
52+
}
53+
54+
{% endhighlight %}
55+
56+
57+
_NOTE: The above points state that interfaces have very little flexibility in how the methods and variables
58+
defined in the interface are declared. This is also one of the **rare differences between an interface and an
59+
abstract class**._
60+
61+
62+
663
### Var-args
764

865
Use var-args when you want to pass __variable number of arguments__ to a method.
@@ -26,6 +83,7 @@ Let's look at some legal and illegal var-arg declarations:
2683
void doStuff6(String... s, byte b) { } // var-arg must be last
2784
{% endhighlight %}
2885

86+
2987
### Enums
3088

3189
Java lets you restrict a variable to having one of only a few pre-defined values, in
@@ -173,4 +231,12 @@ enum CoffeeSize {
173231
{% endhighlight %}
174232

175233

234+
__Some points to note:__
235+
236+
* You can NEVER invoke an enum constructor directly. The enum constructor
237+
is invoked automatically, with the arguments you define after the constant value.
238+
* Every enum has a static method, values(), that returns an array of the enum's
239+
values in the order they're declared.
240+
241+
176242
{% include responsive_ad.html %}
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,28 @@ From any non-subclass class outside the package | Yes | No
6464
public Record getRecord(int fileNumber, final int recordNumber) {}
6565
{% endhighlight %}
6666

67-
A method can never, ever, ever be marked as both **abstract and final, or both abstract and private**.
67+
**Final** when applied to a method prevents it to be overridden and when applied to a class makes it un-inheritable
68+
i.e, it can never be subclassed (no class can extend it).
69+
70+
An **abstract method** is a method that's been declared (as abstract) but not implemented. In other words, the method
71+
contains no functional code. And the class which contains at least one of such methods is an **abstract class** and has to
72+
be declared abstract. An abstract class can **never be instantiated**.
73+
74+
A method can never, ever, ever be marked as **both abstract and final, or both abstract and private or both abstract
75+
and static**.
76+
77+
A class having even a single abstract method has to be declared **abstract** or if it extends an abstract class then
78+
it must implement all abstract methods of the superclass otherwise you have make it abstract as well.
6879

6980
#### Comparison of modifiers on variables vs. methods:
7081

7182
Local Variables | Non-local Variables | Methods
7283
------------------ | ----------------------- | --------
73-
final | final | final
74-
| public | public
84+
final | final | final
85+
| public | public
7586
| protected | protected
7687
| private | private
77-
| static | static
88+
| static | static
7889
| transient | abstract
7990
| volatile | synchronized
8091
| | strictfp

0 commit comments

Comments
 (0)