the oo in java part 2
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/1.jpg)
The OO in JAVA Part 2
![Page 2: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/2.jpg)
Contents:
I. Inheritance
II. Final Methods and Classes
III. Abstract Methods and Classes
IV. Interfaces
V. Polymorphism
VI. Generating UML Diagrams using NETBEANS
![Page 3: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/3.jpg)
![Page 4: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/4.jpg)
Inheritance
![Page 5: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/5.jpg)
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
![Page 6: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/6.jpg)
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
![Page 7: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/7.jpg)
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.
![Page 8: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/8.jpg)
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
![Page 9: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/9.jpg)
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.
![Page 10: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/10.jpg)
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
![Page 11: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/11.jpg)
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.
![Page 12: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/12.jpg)
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.
![Page 13: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/13.jpg)
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.
![Page 14: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/14.jpg)
INHERITANCE
Rules of Inheritance: EXTENSION
1. A class can extend only one other class.
![Page 15: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/15.jpg)
INHERITANCE
Inheritance example:
![Page 16: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/16.jpg)
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.
![Page 17: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/17.jpg)
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.
![Page 18: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/18.jpg)
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?
![Page 19: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/19.jpg)
“ ”
![Page 20: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/20.jpg)
“The super Keyword”
![Page 21: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/21.jpg)
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.
![Page 22: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/22.jpg)
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.
![Page 23: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/23.jpg)
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.
![Page 24: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/24.jpg)
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.
![Page 25: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/25.jpg)
Super
![Page 26: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/26.jpg)
Super
![Page 27: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/27.jpg)
“___ lap, ___ countdown, ___ destination”
![Page 28: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/28.jpg)
Final Methods & Classes
![Page 29: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/29.jpg)
FINAL Methods & Classes
Final Methods:
– Methods which cannot be overridden– In class Monster:
– FireMonster:
![Page 30: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/30.jpg)
FINAL Methods & Classes
Final Classes:
– Classes which cannot be extended
![Page 31: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/31.jpg)
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.
![Page 32: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/32.jpg)
![Page 33: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/33.jpg)
Abstract Classes & Methods
![Page 34: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/34.jpg)
Abstract Classes & Methods
Abstract Class:
– class that is declared abstract – may or may not include abstract methods
![Page 35: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/35.jpg)
Abstract Classes & Methods
Abstract Method:
– method that is declared without an implementation (without braces, and followed by a semicolon)
![Page 36: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/36.jpg)
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.
![Page 37: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/37.jpg)
Abstract Classes & Methods
![Page 38: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/38.jpg)
Abstract Classes & Methods
![Page 39: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/39.jpg)
Abstract Classes & Methods
![Page 40: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/40.jpg)
Abstract Classes & Methods
Question:
– Why use abstract classes and methods?
![Page 41: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/41.jpg)
Abstract Classes & Methods
![Page 42: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/42.jpg)
Bomba Packers
Explosives Exclusive, Inc.
at
Usapang
![Page 43: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/43.jpg)
Bomba Packers
Explosives Exclusive, Inc.
Hi, gagawa ako ng Liquid Nitrogen Bomb software. Pagawa sana ako bomb
packaging na makakapag-activate ng software ko.
![Page 44: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/44.jpg)
Bomba Packers
Explosives Exclusive, Inc.
Hmmm, okay. Paano mo ba gagawin yung
software? E-mail mo sa akin yung code mo para
sure…
![Page 45: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/45.jpg)
Bomba Packers
Explosives Exclusive, Inc.
Huh? E, medyo private yung code ko eh… You know, proprietary stuff
and all… Paano na?
![Page 46: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/46.jpg)
Bomba Packers
Explosives Exclusive, Inc.
Heller! Paano ko naman maa-activate yan kung wala akong idea paano
nag-wowork ung software mo? Sira…
![Page 47: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/47.jpg)
Bomba Packers
Explosives Exclusive, Inc.
Hmmm…
![Page 48: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/48.jpg)
Bomba Packers
Explosives Exclusive, Inc.
Hmmm…
![Page 49: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/49.jpg)
Bomba Packers
Explosives Exclusive, Inc.
Ahah! :)Aaah! :)
![Page 50: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/50.jpg)
Bomba Packers
Explosives Exclusive, Inc.
Agree tayo on something…
![Page 51: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/51.jpg)
Bomba Packers
Explosives Exclusive, Inc.
Oo nga… Yan din nasa isip ko!
![Page 52: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/52.jpg)
Bomba Packers
Explosives Exclusive, Inc.
Basta ito yung kelangan mo i-call na methods:
explode()arm()
disarm()startTimer()
![Page 53: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/53.jpg)
Bomba Packers
Explosives Exclusive, Inc.
Kewl. Ok.
![Page 54: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/54.jpg)
Interfaces
![Page 55: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/55.jpg)
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.
![Page 56: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/56.jpg)
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
![Page 57: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/57.jpg)
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.
![Page 58: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/58.jpg)
INTERFACES
Major Benefits:
1. Interfaces as API (Application Programming Interface)
2. Multiple Inheritance
3. Polymorphism
![Page 59: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/59.jpg)
INTERFACES DEMO
• Defining an Interface
• Implementing an Interface
• Creating Interaction Form using Interfaces
![Page 60: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/60.jpg)
![Page 61: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/61.jpg)
Polymorphism
![Page 62: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/62.jpg)
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.
![Page 63: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/63.jpg)
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.
![Page 64: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/64.jpg)
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.
![Page 65: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/65.jpg)
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.
![Page 66: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/66.jpg)
Review:
I. Inheritance
II. Final Methods and Classes
III. Abstract Methods and Classes
IV. Interfaces
V. Polymorphism
VI. Generating UML Diagrams Using NETBEANS
![Page 67: The OO in JAVA Part 2](https://reader036.vdocument.in/reader036/viewer/2022062309/568150a5550346895dbeaca1/html5/thumbnails/67.jpg)
End of OO in Java…OO end na…