1 / 67

The OO in JAVA Part 2

The OO in JAVA Part 2. Contents:. Inheritance Final Methods and Classes Abstract Methods and Classes Interfaces Polymorphism Generating UML Diagrams using NETBEANS. Inheritance. Inheritance. Inheritance refers to the ability to define new classes from existing class definitions.

fawzi
Download Presentation

The OO in JAVA Part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The OO in JAVA Part 2

  2. Contents: • Inheritance • Final Methods and Classes • Abstract Methods and Classes • Interfaces • Polymorphism • Generating UML Diagrams using NETBEANS

  3. Inheritance

  4. Inheritance • Inheritance refers to the ability to define new classes from existing class definitions. • Sub-classing. • Parent class – child class • Child classes inherit all the inheritable properties, events and methods of the parent class. • Where it is permissible may override the parent class implementation

  5. Inheritance • Override – is the act of using the same method from the parent class and implementing it differently. The argument list and the return type are the same. • Inheritance makes objects highly reusable as existing functionality is extended and not duplicated across modules. • Base class, superclass or parent class • Subclass, derived class or child class

  6. Inheritance • Inheritance is on of the foundation principles of object-oriented programming because it allows the creation of hierarchical classifications. • Using inheritance, you can create a general class that defines traits common to a set of related items. • This class can then be inherited by other, more specific classes, each adding those things that are unique to it.

  7. Inheritance • A class that is inherited is called the superclass, base class or parent class • The class that does the inheriting is called a sub class, child class or derived class

  8. INHERITANCE • a way to form new classes using classes that have already been defined • In Java, all classes, including the classes that make up the Java API, are subclassed from the Object superclass.

  9. INHERITANCE • Superclass – Any class above a specific reference class in the class hierarchy. • Subclass – Any class below a specific reference class in the class hierarchy. • Class members are inherited FROM a superclass BY a subclass

  10. INHERITANCE Benefits of Inheritance • Once a behavior (method) is defined in a superclass, that behavior is automatically inherited by all subclasses. • A method can be encoded only once and it can be used by all subclasses. • A subclass only needs to implement the differences between itself and the parent.

  11. INHERITANCE Rules of Inheritance: MEMBERS • A subclass inherits ALL the members (fields, methods, and nested classes) from its superclass. However, only the public and protected can be accessed directly from within the subclass. • Constructors are not members, so they are not inherited by subclasses. • The constructor of the superclass can be invoked from the subclass.

  12. INHERITANCE Rules of Inheritance: PRIVATE MEMBERS • A subclass does not inherit the private members of its parent class, at least directly. • Use the inherited public or protected methods to access the private members.

  13. INHERITANCE Rules of Inheritance: EXTENSION • A class can extend only one other class. 

  14. INHERITANCE Inheritance example:

  15. INHERITANCE Rules of Inheritance: FIELDS • Inherited non-private fields can be used directly, just like any other field. • A field in the subclass declared with the same name as the one in the superclass hides the superclass’s (not recommended). (Field Hiding) • New fields that are not in the superclass can be declared in the subclass.

  16. INHERITANCE Rules of Inheritance: METHODS • Inherited methods can be used directly as they are. • A new instancemethod in the subclass that has the samesignature as the one in the superclass overrides the superclass’s. Method Overriding • A new staticmethod in the subclass that has the samesignature as the one in the superclass hides the superclass’s. Method Hiding • The access specifier for an overriding method can allow more, but not less, access than the overridden method. • New methods that are not in the superclass can be declared in the subclass.

  17. Progress Check • When creating a subclass, what keyword is used to include a superclass? • Does a subclass include the members of its superclass? • Does a subclass have access to private members of its superclass?

  18. “ ”

  19. “The super Keyword”

  20. super KEYWORD When to use your super? • Invoke overridden method. • Refer to a hidden field. • Invoke the superclass constructor. • Refer to members of the superclass. NOTE: • If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.

  21. super KEYWORD RULES for superclass constructor calls: • The super() call must occur as the first statement in a constructor. • The super() call can only be used in a constructor definition. • This implies that the this() construct and the super() calls cannot both occur in the same constructor.

  22. super KEYWORD When are superclass constructor calls usually used? • You want to call a parent constructor which has parameters (the automatically generated super constructor call has no parameters). • There is no parameterless parent constructor because only constructors with parameters are defined in the parent class.

  23. super KEYWORD Key concepts behind super • When a subclass calls super(), it is calling the constructor of its immediate super class. • Super() always refers to the superclass immediately above the calling class. • This is true in a multilevel heirarchy. • Super() must always be the first statement executed inside a subclass constructor.

  24. Super

  25. Super

  26. “___ lap, ___ countdown, ___ destination”

  27. Final Methods & Classes

  28. FINAL Methods & Classes Final Methods: • Methods which cannot be overridden • In class Monster: • FireMonster:

  29. FINAL Methods & Classes Final Classes: • Classes which cannot be extended

  30. FINAL Methods & Classes When to make your method orclass final? • Implementation of the method is critical to the consistent state of the object and should not be changed. • Methods called from constructors should generally be declared final.

  31. Abstract Classes & Methods

  32. Abstract Classes & Methods Abstract Class: • class that is declared abstract • may or may not include abstract methods

  33. Abstract Classes & Methods Abstract Method: • method that is declared without an implementation (without braces, and followed by a semicolon)

  34. Abstract Classes & Methods Rules: • If a class includes abstract methods, the class itself must be declared abstract. • An abstract class cannot be instantiated, but they can be subclassed. • You cannot declare an instance of an abstract class. • An abstract class must be inherited by a subclass in order for its data and behavior to be used in an application. • When a non-abstract class extends an abstract class, the former should implement all abstract methods of the latter. • When an abstractclass extends an abstract class, the subclass may implement all, some, or none of the abstract methods of the superclass.

  35. Abstract Classes & Methods

  36. Abstract Classes & Methods

  37. Abstract Classes & Methods

  38. Abstract Classes & Methods Question: • Why use abstract classes and methods? 

  39. Abstract Classes & Methods

  40. Usapang Explosives Exclusive, Inc. at Bomba Packers

  41. Explosives Exclusive, Inc. Hi, gagawa ako ng Liquid Nitrogen Bomb software. Pagawa sana ako bomb packaging na makakapag-activate ng software ko. Bomba Packers

  42. Explosives Exclusive, Inc. Hmmm, okay. Paano mo ba gagawin yung software? E-mail mo sa akin yung code mo para sure… Bomba Packers

  43. Explosives Exclusive, Inc. Huh? E, medyo private yung code ko eh… You know, proprietary stuff and all… Paano na? Bomba Packers

  44. Explosives Exclusive, Inc. Heller! Paano ko naman maa-activate yan kung wala akong idea paano nag-wowork ung software mo? Sira… Bomba Packers

  45. Explosives Exclusive, Inc. Hmmm… Bomba Packers

  46. Explosives Exclusive, Inc. Hmmm… Bomba Packers

  47. Explosives Exclusive, Inc. Ahah! :) Aaah! :) Bomba Packers

  48. Explosives Exclusive, Inc. Agree tayo on something… Bomba Packers

More Related