670 likes | 826 Views
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.
E N D
Contents: • Inheritance • Final Methods and Classes • Abstract Methods and Classes • Interfaces • Polymorphism • Generating UML Diagrams using NETBEANS
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 • 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.
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.
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.
INHERITANCE Rules of Inheritance: EXTENSION • A class can extend only one other class.
INHERITANCE Inheritance example:
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.
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.
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?
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.
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.
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.
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.
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 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.
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: • 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.
Abstract Classes & Methods Question: • Why use abstract classes and methods?
Usapang Explosives Exclusive, Inc. at 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