KEMBAR78
More oop in java | PPTX
More OOP in JAVA
Abstract Classes & Methods
• Data abstraction is the process of hiding certain details and showing only essential
information to the user.
• Abstraction can be achieved with either abstract classes or interfaces.
• The abstract keyword is a non-access modifier, used for classes and methods:
◦ Abstract class: is a restricted class that cannot be used to create objects (to access it, it
must be inherited from another class).
◦ Abstract method: can only be used in an abstract class, and it does not have a body. The
body is provided by the subclass (inherited from).
Abstract Classes & Methods
• An Abstract Class is a class that is declared abstract.
• When an abstract class is subclass, the subclass usually provides implementations for all the
abstract methods in its parent class. However, if it does not, then the subclass must also be
declared abstract.
• An Abstract Method is a method that is declared without an implementation (without braces,
and followed by a semicolon), like this:
◦ abstract void moveTo(double deltaX, double deltaY);
• An abstract class can have both abstract and regular methods:
• From the example above, it is not possible to create an object of the Animal class:
• To access the abstract class, it must be inherited from another class.
Example of
Abstract Class
• Remember from
the inheritance
chapter that we
use the extends
keyword to inherit
from a class.
Use Abstract Classes & Methods
Why and when to use abstract classes &
methods?
•To achieve security - hide certain details
and only show the important details of an
object.
Note: Abstraction can also be achieved
with interfaces.
JAVA Interfaces
• Like abstract classes, interfaces cannot be used to create objects.
• Interface methods do not have a body - the body is provided by the implement class.
• On implementation of an interface, you must override all its methods.
• Interface methods are by default abstract and public.
• Interface attributes are by default public, static, and final.
• An interface cannot contain a constructor (as it cannot be used to create objects).
JAVA Interfaces
• Another way to achieve abstraction in Java, is with interfaces.
• An interface is a completely abstract class that is used to group related methods with empty
bodies:
• To access the interface methods, the interface must be implemented (kind like inherited) by
another class with the implements keyword (instead of extends). The body of the interface
method is provided by the implement class.
Example of
Interface
Multiple
Interfaces
• To implement
multiple
interfaces,
separate them
with a comma.
Final Keyword
• The Final keyword is a non-access modifier used for classes, attributes and methods; which
makes them non-changeable (impossible to inherit or override).
• The Final keyword is useful when you want a variable to always store the same value, like PI
(3.14159...).
• If you make any variable as Final, you cannot change the value of final variable (It will be
constant).
• If you make any method as final, you cannot override it.
• If you make any class as final, you cannot extend it.
Final Keyword for Variable
• Set a variable to final, to prevent it from being overridden/modified.
Final Class Example
• If we extends final class, it shows Error.
Thank You

More oop in java

  • 1.
  • 2.
    Abstract Classes &Methods • Data abstraction is the process of hiding certain details and showing only essential information to the user. • Abstraction can be achieved with either abstract classes or interfaces. • The abstract keyword is a non-access modifier, used for classes and methods: ◦ Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). ◦ Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).
  • 3.
    Abstract Classes &Methods • An Abstract Class is a class that is declared abstract. • When an abstract class is subclass, the subclass usually provides implementations for all the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract. • An Abstract Method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: ◦ abstract void moveTo(double deltaX, double deltaY);
  • 4.
    • An abstractclass can have both abstract and regular methods: • From the example above, it is not possible to create an object of the Animal class: • To access the abstract class, it must be inherited from another class.
  • 5.
    Example of Abstract Class •Remember from the inheritance chapter that we use the extends keyword to inherit from a class.
  • 6.
    Use Abstract Classes& Methods Why and when to use abstract classes & methods? •To achieve security - hide certain details and only show the important details of an object. Note: Abstraction can also be achieved with interfaces.
  • 7.
    JAVA Interfaces • Likeabstract classes, interfaces cannot be used to create objects. • Interface methods do not have a body - the body is provided by the implement class. • On implementation of an interface, you must override all its methods. • Interface methods are by default abstract and public. • Interface attributes are by default public, static, and final. • An interface cannot contain a constructor (as it cannot be used to create objects).
  • 8.
    JAVA Interfaces • Anotherway to achieve abstraction in Java, is with interfaces. • An interface is a completely abstract class that is used to group related methods with empty bodies: • To access the interface methods, the interface must be implemented (kind like inherited) by another class with the implements keyword (instead of extends). The body of the interface method is provided by the implement class.
  • 9.
  • 10.
  • 11.
    Final Keyword • TheFinal keyword is a non-access modifier used for classes, attributes and methods; which makes them non-changeable (impossible to inherit or override). • The Final keyword is useful when you want a variable to always store the same value, like PI (3.14159...). • If you make any variable as Final, you cannot change the value of final variable (It will be constant). • If you make any method as final, you cannot override it. • If you make any class as final, you cannot extend it.
  • 12.
    Final Keyword forVariable • Set a variable to final, to prevent it from being overridden/modified.
  • 13.
    Final Class Example •If we extends final class, it shows Error.
  • 14.