Difference between revisions of "Java"

From wiki
Jump to navigation Jump to search
Line 24: Line 24:
 
* A 'void' method is a method with no return value, else the return value type must be specified
 
* 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).
 
* 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>

Revision as of 14:34, 17 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).

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>() {
    }
}