Download - The OO in JAVA Part 2
The OO in JAVA Part 2
Contents:
I. Inheritance
II. Final Methods and Classes
III. Abstract Methods and Classes
IV. Interfaces
V. Polymorphism
VI. Generating UML Diagrams using NETBEANS
Inheritance
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
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
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.
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
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.
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
INHERITANCE
Benefits of Inheritance
1. Once a behavior (method) is defined in a superclass, that behavior is automatically inherited by all subclasses.
2. A method can be encoded only once and it can be used by all subclasses.
3. A subclass only needs to implement the differences between itself and the parent.
INHERITANCE
Rules of Inheritance: MEMBERS
1. 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.
2. Constructors are not members, so they are not inherited by subclasses.
3. The constructor of the superclass can be invoked from the subclass.
INHERITANCE
Rules of Inheritance: PRIVATE MEMBERS
1. A subclass does not inherit the private members of its parent class, at least directly.
2. Use the inherited public or protected methods to access the private members.
INHERITANCE
Rules of Inheritance: EXTENSION
1. A class can extend only one other class.
INHERITANCE
Inheritance example:
INHERITANCE
Rules of Inheritance: FIELDS
1. Inherited non-private fields can be used directly, just like any other field.
2. A field in the subclass declared with the same name as the one in the superclass hides the superclass’s (not recommended). (Field Hiding)
3. New fields that are not in the superclass can be declared in the subclass.
INHERITANCE
Rules of Inheritance: METHODS
1. Inherited methods can be used directly as they are.
2. A new instance method in the subclass that has the same signature as the one in the superclass overrides the superclass’s. Method Overriding
3. A new static method in the subclass that has the same signature as the one in the superclass hides the superclass’s. Method Hiding
4. The access specifier for an overriding method can allow more, but not less, access than the overridden method.
5. New methods that are not in the superclass can be declared in the subclass.
Progress Check
1. When creating a subclass, what keyword is used to include a superclass?
2. Does a subclass include the members of its superclass?
3. Does a subclass have access to private members of its superclass?
“ ”
“The super Keyword”
super KEYWORD
When to use your super?
1. Invoke overridden method.
2. Refer to a hidden field.
3. Invoke the superclass constructor.
4. 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.
super KEYWORD
RULES for superclass constructor calls:
1. The super() call must occur as the first statement in a constructor.
2. The super() call can only be used in a constructor definition.
3. This implies that the this() construct and the super() calls cannot both occur in the same constructor.
super KEYWORD
When are superclass constructor calls usually used?
1. You want to call a parent constructor which has parameters (the automatically generated super constructor call has no parameters).
2. There is no parameterless parent constructor because only constructors with parameters are defined in the parent class.
super KEYWORD
Key concepts behind super
1. When a subclass calls super(), it is calling the constructor of its immediate super class.
2. Super() always refers to the superclass immediately above the calling class.
3. This is true in a multilevel heirarchy.4. Super() must always be the first statement executed
inside a subclass constructor.
Super
Super
“___ lap, ___ countdown, ___ destination”
Final Methods & Classes
FINAL Methods & Classes
Final Methods:
– Methods which cannot be overridden– In class Monster:
– FireMonster:
FINAL Methods & Classes
Final Classes:
– Classes which cannot be extended
FINAL Methods & Classes
When to make your method or class final?
1. Implementation of the method is critical to the consistent state of the object and should not be changed.
2. Methods called from constructors should generally be declared final.
Abstract Classes & Methods
Abstract Classes & Methods
Abstract Class:
– class that is declared abstract – may or may not include abstract methods
Abstract Classes & Methods
Abstract Method:
– method that is declared without an implementation (without braces, and followed by a semicolon)
Abstract Classes & Methods
Rules:
1. If a class includes abstract methods, the class itself must be declared abstract.
2. An abstract class cannot be instantiated, but they can be subclassed.
3. You cannot declare an instance of an abstract class.4. An abstract class must be inherited by a subclass in order for
its data and behavior to be used in an application.
5. When a non-abstract class extends an abstract class, the former should implement all abstract methods of the latter.
6. When an abstract class extends an abstract class, the subclass may implement all, some, or none of the abstract methods of the superclass.
Abstract Classes & Methods
Abstract Classes & Methods
Abstract Classes & Methods
Abstract Classes & Methods
Question:
– Why use abstract classes and methods?
Abstract Classes & Methods
Bomba Packers
Explosives Exclusive, Inc.
at
Usapang
Bomba Packers
Explosives Exclusive, Inc.
Hi, gagawa ako ng Liquid Nitrogen Bomb software. Pagawa sana ako bomb
packaging na makakapag-activate ng software ko.
Bomba Packers
Explosives Exclusive, Inc.
Hmmm, okay. Paano mo ba gagawin yung
software? E-mail mo sa akin yung code mo para
sure…
Bomba Packers
Explosives Exclusive, Inc.
Huh? E, medyo private yung code ko eh… You know, proprietary stuff
and all… Paano na?
Bomba Packers
Explosives Exclusive, Inc.
Heller! Paano ko naman maa-activate yan kung wala akong idea paano
nag-wowork ung software mo? Sira…
Bomba Packers
Explosives Exclusive, Inc.
Hmmm…
Bomba Packers
Explosives Exclusive, Inc.
Hmmm…
Bomba Packers
Explosives Exclusive, Inc.
Ahah! :)Aaah! :)
Bomba Packers
Explosives Exclusive, Inc.
Agree tayo on something…
Bomba Packers
Explosives Exclusive, Inc.
Oo nga… Yan din nasa isip ko!
Bomba Packers
Explosives Exclusive, Inc.
Basta ito yung kelangan mo i-call na methods:
explode()arm()
disarm()startTimer()
Bomba Packers
Explosives Exclusive, Inc.
Kewl. Ok.
Interfaces
INTERFACES
Generally speaking, interfaces are referred to as “contracts”…
…that spell out how the software creations of disparate groups of programmers interact…
…in such a way that each group should be able to write their code without any knowledge of how the other group's code is written.
INTERFACES
• a reference type, similar to a class
• can contain only constants, method signatures, and nested types
• NO method bodies
• CANNOT be instantiated
• CAN be implemented
• CAN be extended but only by other interfaces
Abstract Classes & Methods
Rules:
1. A class can implement more than one interface.
2. An interface can extend any number of other interfaces.
3. All methods declared in an interface are implicitly public, so the public modifier can be omitted.
4. All constant values defined in an interface are implicitly public, static, and final.
INTERFACES
Major Benefits:
1. Interfaces as API (Application Programming Interface)
2. Multiple Inheritance
3. Polymorphism
INTERFACES DEMO
• Defining an Interface
• Implementing an Interface
• Creating Interaction Form using Interfaces
Polymorphism
POLYMORPHISM
• The ability of a reference variable to change behavior according to what object it is holding.
• This allows multiple objects of different subclasses to be treated as objects of a single superclass, while automatically selecting the proper methods to apply to a particular object based on the subclass it belongs to.
• A superclass can directly morph into anything below it in the class hierarchy.
• An interface can directly morph into anything that implements it.
POLYMORPHISM
• The ability to exhibit differing behavior given the same interface.
• It provides flexibility since objects can define their own unique implementations of the same interface.
• Polymorphism promotes code simplicity and consistency since a uniform set of interfaces can be used across different objects, and yet client code can treat these objects as the same.
UML Diagrams Using NETBEANS
• In order to generate UML diagrams you must first download the module for UML
• Go to Plugins and then select UML and then download it.
• Once you have downloaded it you will be able to see UML in one of the folders of add new project.
• Try adding a new project.• Select an existing project that you have
created, for example your MonsterProject.
UML Diagrams Using NETBEANS
• Expand the green folders in the project• In order to generate a class diagram highlight
the classes and then right click on them.• Select create diagram from selected elements• Afterwards you can expand one of the classes
to view their operations• Select one of the methods available and then
right click it.• Select reverse engineer operation.• You can now generate a sequence diagram.
Review:
I. Inheritance
II. Final Methods and Classes
III. Abstract Methods and Classes
IV. Interfaces
V. Polymorphism
VI. Generating UML Diagrams Using NETBEANS
End of OO in Java…OO end na…