Вы находитесь на странице: 1из 5

Basic class:

1. Use class keyword to create class


class <class-name> {
}
2. Save file as class-name.java
3. javac –d . class-name.java
4. it will create the respective package (it it nothing but folder)
5. java class-name
6. you will get the output

e.g

package p4;

class Test{

public static void main(String[] args){

System.out.println(“Hello”);

}
Javap –d . Test.java

p4

Test.clas
s
java p4.Test

Access Modifier:

1. public: it is accessible anywhere


2. private: it is accessible within class only
3. protected :
For protected access modifier we need to understand the relation
between two classes.
With the help of extends keyword, classes can related with each
other. That relation will be parent and child or main class and
subclass.
The class which inherit (acquire) properties (variables) and
behavior (methods) of another class is called as child class and the
class which inherited (acquired) is called as parent class.
Within the package public, protected and default are accessible
anywhere. But outside package only public and protected (if have
relation child)

4. <default>: when you never any access modifier then it is consider


as default.

Access Within Within Child Other Outside Outside


Modifier class package within class package package
packag within child Other
e package class class
public Yes Yes Yes Yes Yes Yes
protecte Yes Yes Yes Yes Yes No
d
default Yes Yes Yes Yes No No
private Yes No No No no no

Most-accessible------------------------------lease accessible
public > protected > default >private
Inner class and Outer class:

class A{

class B{

Outer class – A

Inner class ---B

After compilation of nested class we get two class files having name as
below

E:\Priyansh_Java_Prog>javac nestedClassEx01.java

08/05/2020 09:20 PM A$B.class

08/05/2020 09:20 PM A.class

Acess Modifier Outer Class (class A) Inner class (class B)


public Yes Yes
<default> Yes Yes
protected No Yes
private No Yes
abstract Yes Yes
final Yes Yes
strictfp Yes yes

Method:
- it is like function present in c and cpp.

Method is divided into three part:

 Method prototype
 Method signature
 Method call

Def: block of code with name and having parameter as input and it
will return something or not is called as method.

e.g.

void m1(int a, int b){

int c;

c=a+b;

System.out.println(“Addition is :”+c);

abstract keyword:

- meaning of abstract is incomplete

Вам также может понравиться