11.java
11.java
4.Abstraction:
•Abstract Methods:
>declared but not defined methods(no body).
------------------------------------------------------
public abstract type METHODNAME(types parameters);
------------------------------------------------------
•Abstract Classes:
>class that contains abstract methods (or concrete=non abstract methods).
-----------------------------------
abstract class CLASSNAME{ ... }
-----------------------------------
>Declaration(example):
--------------------------------------------------
public abstract class Shape
{
public abstract void draw();
}
public class Circle extends Shape
{
public void draw()
{
System.out.println("drawing a circle");
}
---------------------------------------------------
-----------------------------------------------------------------------------------
----
>Declaration:
---------------------------------------------------
public interface INTERFACENAME
{
public static final type VARNAME = value;
public abstract type METHODNAME();
}
----------------------------------------------------
->for variables: (public static final) added by compiler.
->for methods: (public abstract) added by compiler.
-----------
•Implementation:
>Interfaces are implemented (like classes are extended).
----------------------------------------------------
public class CLASSNAME implements INTERFACENAME
{
public type METHODNAME()
{
statements;
}
}
----------------------------------------------------
-----------
-----------
•Interfaces in Java 8:
-Default Methods:
>added to Interface with declaration(have a body).
>classes that implement the interface do not have to define default
methods.
>method functionality belongs to all classes implementing the interface.
----------------------------------------------
public interface Person
{
default void Greeting()
{
System.out.print("hello");
}
}
public class Test implements Person{}
-----------------------------------------------
in MAIN method:
Test P1 = new Person()
P1.Greeting();
-----------------------------------------------
Output--> hello
-----------------------------------------------
---------
-Static Methods:
>don't belong to particular object.
>not part of API of classes implementing the interface.
----------------------------------------------
public interface Person
{
static void Greeting()
{
System.out.print("hi");
}
}
-----------------------------------------------
in MAIN method:
Person.Greeting();
-----------------------------------------------
Output--> hi
-----------------------------------------------
---------
-Comparable Interface:
>order the objects of the user-defined class based on 1 attribute.
-----------------------------------------------
int compareTo (Object obj)
-----------------------------------------------
->compare the current object to specified object(in parameter)
returns:
•positive int: current object > specified object
•negative int: current object < specified object
•0: current object = specified object