Java: Difference between revisions

From wiki
Jump to navigation Jump to search
Created page with "20220531 Fist notes on java progamming. <syntaxhighlight lang=java> public class HelloWorld { public static void main(String args[] ) { String text = "hello world"; Sys..."
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
20220531
20220531


Fist notes on java progamming.
Fist notes on java programming.


<syntaxhighlight lang=java>
<syntaxhighlight lang=java>
Line 15: Line 15:
=Variables=
=Variables=
;int var1 = 0
;int var1 = 0
:Variables must be declared and initialised
:Variables must be declared and initialized
 
;private int var1;
:Private variables in classes can only be accessed from within the class (same for methods).
 
=Access levels=
* Public all can use it. Public attributes can be changes by all
* Protected, can be used within the class, by subclasses and by classes in the same package (A package is a group of related classes in one namespace)
* Default, like protected but no access from subclasses outside the package. If nothing is specified it is Default.
* Private can only be used within the class
 
=Methods=
* Every program must have a main method, that is executed when the program starts
* A 'void' method is a method with no return value, else the return value type must be specified
* Private methods in classes can only be accessed from within the class (same for variables).
 
=Classes=
abstract classes cannot be instantiated by itself it is only a superclass for its subclasses.
 
;public class <subclass> extends <superclass>
:Instantiate a subclass object
 
<syntaxhighlight lang=java>
public class <subclass> extends <superclass> {
    public <subclass> (<types and attributes from superclass>)
        super(<attributes defined in superclass>)
    }
    private <type> <subclass specific attribute>
 
  public void <subclass specific method>() {
  }
</syntaxhighlight>
 
Superclasses can have 'protected' attributes and methods making them available for subclasses and in the same package.
 
Methods and attributes in subclasses can override the superclass methods and attributes.
 
=Interfaces=
A bit like subclasses but only describing the methods used from the superclass. It does not implement the method but only declares it. The implementation is in the subclass. A subclass can implement multiple interfaces unlike inheritance that can have only 1 superclass.
 
Interface can extend other interfaces.
 
<syntaxhighlight lang=java>
public class <subclass> implements <interface> {
    <attribute declarations>
   
    public void <method from interface>() {
    }
}
</syntaxhighlight>

Latest revision as of 18:52, 28 June 2022

20220531

Fist notes on java programming.

public class HelloWorld {
	public static void main(String args[] ) {
		String text = "hello world";
		System.out.println( text );
	}
}

If a class implements an interface all methods in that interface must be defined in the class

Variables

int var1 = 0
Variables must be declared and initialized
private int var1;
Private variables in classes can only be accessed from within the class (same for methods).

Access levels

  • Public all can use it. Public attributes can be changes by all
  • Protected, can be used within the class, by subclasses and by classes in the same package (A package is a group of related classes in one namespace)
  • Default, like protected but no access from subclasses outside the package. If nothing is specified it is Default.
  • Private can only be used within the class

Methods

  • Every program must have a main method, that is executed when the program starts
  • A 'void' method is a method with no return value, else the return value type must be specified
  • Private methods in classes can only be accessed from within the class (same for variables).

Classes

abstract classes cannot be instantiated by itself it is only a superclass for its subclasses.

public class <subclass> extends <superclass>
Instantiate a subclass object
public class <subclass> extends <superclass> {
    public <subclass> (<types and attributes from superclass>)
        super(<attributes defined in superclass>)
    }
    private <type> <subclass specific attribute>

   public void <subclass specific method>() {
   }

Superclasses can have 'protected' attributes and methods making them available for subclasses and in the same package.

Methods and attributes in subclasses can override the superclass methods and attributes.

Interfaces

A bit like subclasses but only describing the methods used from the superclass. It does not implement the method but only declares it. The implementation is in the subclass. A subclass can implement multiple interfaces unlike inheritance that can have only 1 superclass.

Interface can extend other interfaces.

public class <subclass> implements <interface> {
    <attribute declarations>
    
    public void <method from interface>() {
    }
}