OOP CONCEPTS:
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{ ... }
-----------------------------------
>can have constant and variable data.
>incomplete class-->has missing method bodies.
>cannot instantiate an object from an abstract class.
>it can be extended (have subclasses):
-subclass defines all inherited abstract methods->SUPERCLASS is complete.
->can be instantiated.
-subclass don't define all inherited abstract methods
->SUBCLASS must be ABSTRACT too.
->ABSTRACT class with no abstract methods:
>prevent the class from being instantiated (having objects).
>Creation of a GENERAL category (that contains specific subclasses).
--------------
>Declaration(example):
--------------------------------------------------
public abstract class Shape
{
public abstract void draw();
}
public class Circle extends Shape
{
public void draw()
{
System.out.println("drawing a circle");
}
---------------------------------------------------
-----------------------------------------------------------------------------------
----
-INTERFACES: (reference type, similar to classes)
>describes only public abstract methods ->(like a very abstract class).
>cannot instanciate variables and objects.
>may contain constants =>public + static + final variables(by default).
->must be initializes at declaration.
>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).
>class implements an interface:
-it defines all methods declared in the interface.
-contract that classes should respect(security of implementation).
----------------------------------------------------
public class CLASSNAME implements INTERFACENAME
{
public type METHODNAME()
{
statements;
}
}
----------------------------------------------------
-----------
-->Interfaces support Multiple Inheritance functionality (unlike classes)
--Interface can EXTEND multiple Interfaces.
-Interface cannot extend a class.
--Class can IMPLEMENT multiple Interfaces.
-Class can extend only 1 class.
-----------
•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.
->create object in MAIN method to call the default method.
----------------------------------------------
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.
->methods called by using the Interface name.
----------------------------------------------
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