Unit 3 PPT Slides Text Books
Unit 3 PPT Slides Text Books
UNIT 3 PPT SLIDES TEXT BOOKS: 1. Java: the complete reference, 7th edition, Herbert schildt, TMH.Understanding 2. OOP with Java, updated edition, T. Budd, Pearson education.
No. of slides:66
S.NO.
1 2 3 4 5 6
TOPIC
LECTURE NO.
PPTSLIDES
Hierarchical abstractions Base class object. subclass, subtype, substitutability. forms of inheritance- specialization, specification. construction, extension, limitation, combination. Benefits of inheritance, costs of inheritance. Member access rules, super uses, using final with inheritance. polymorphism- method overriding, abstract classes.
L1 L2 L3 L4 L5 L6 L7
L1.1TO L1.9 L2.1 TO L2.8 L3.1 TO L3.5 L4.1 TO L4.9 L5.1 TO 5.4 L6.1 TO 6.17 L7.1 TO 7.11
Hierarchical Abstraction
An essential element of object-oriented programming is abstraction. Humans manage complexity through abstraction. For example, people do not think of a car as a set of tens of thousands of individual parts. They think of it as a welldefined object with its own unique behavior. This abstraction allows people to use a car without being overwhelmed by the complexity of the parts that form the car. They can ignore the details of how the engine, transmission, and braking systems work. Instead they are free to utilize the object as a whole.
L 1.1
Class Hierarchy
A child class of one parent can be the parent of another child, forming class hierarchies
Animal
Reptile
Bird
Mammal
Snake
Lizard
Parrot
Horse
Bat
At the top of the hierarchy theres a default class called Object. L 1.2
Class Hierarchy
Good class design puts all common features as high in the hierarchy as reasonable The class hierarchy determines how methods are executed
inheritance is transitive An instance of class Parrot is also an instance of Bird, an instance of Animal, , and an instance of class Object
L 1.3
In Java, all classes use inheritance. If no parent class is specified explicitly, the base class Object is implicitly inherited. All classes defined in Java, is a child of Object class, which provides minimal functionality guaranteed to e common to all objects.
L 1.4
L 1.5
Base class
1) a class obtains variables and methods from another class 2) the former is called subclass, the latter super-class (Base class) 3) a sub-class provides a specialized behavior with respect to its super-class 4) inheritance facilitates code reuse and avoids duplication of data
L 1.6
One of the pillars of object-orientation. A new class is derived from an existing class: 1) existing class is called super-class 2) derived class is called sub-class A sub-class is a specialized version of its super-class: 1) has all non-private members of its super-class 2) may provide its own implementation of super-class methods Objects of a sub-class are a special kind of objects of a super-class.
L 1.7
extends
Is a keyword used to inherit a class from another class Allows to extend from only one class
One baseobj;// base class object. super class object baseobj can be used to refer its sub class objects. For example, Two subobj=new Two; Baseobj=subobj // now its pointing to sub class
L 1.9
A subtype is a class that satisfies the principle of substitutability. A subclass is something constructed using inheritance, whether or not it satisfies the principle of substitutability. The two concepts are independent. Not all subclasses are subtypes, and (at least in some languages) you can construct subtypes that are not subclasses.
L 2.1
Substitutability is fundamental to many of the powerful software development techniques in OOP. The idea is that, declared a variable in one type may hold the value of different type. Substitutability can occur through use of inheritance, whether using extends, or using implements keywords.
L 2.2
Subclass
Methods allows to reuse a sequence of statements Inheritance allows to reuse classes by deriving a new class from an existing one The existing class is called the parent class, or superclass, or base class The derived class is called the child class or subclass. As the name implies, the child inherits characteristics of the parent(i.e the child class inherits the methods and data defined for the parent class
L 2.5
Subtype
Inheritance relationships are often shown graphically in a class diagram, with the arrow pointing to the parent class
Animal weight : int + getWeight() : int
Bird
+ fly() : void
L 2.6
A child class can override the definition of an inherited method in favor of its own
that is, a child can redefine a method that it inherits from its parent the new method must have the same signature as the parent's method, but can have different code in the body
In java, all methods except of constructors override the methods of their ancestor class by replacement. E.g.:
the Animal class has method eat() the Bird class has method eat() and Bird extends Animal variable b is of class Bird, i.e. Bird b = b.eat() simply invokes the eat() method of the Bird class
Forms of Inheritance
Inheritance is used in a variety of way and for a variety of different purposes . Inheritance for Specialization Inheritance for Specification Inheritance for Construction Inheritance for Extension Inheritance for Limitation Inheritance for Combination One or many of these forms may occur in a single case.
L 3.1
Specialization
By far the most common form of inheritance is for specialization. Child class is a specialized form of parent class Principle of substitutability holds A good example is the Java hierarchy of Graphical components in the AWT: Component Label Button TextComponent TextArea TextField CheckBox ScrollBar
L 3.3
Specification
The next most common form of inheritance involves specification. The parent class specifies some behavior, but does not implement the behavior
Child class implements the behavior Similar to Java interface or abstract class When parent class does not implement actual behavior but merely defines the behavior that will be implemented in child classes Example, Java 1.1 Event Listeners: ActionListener, MouseListener, and so on specify behavior, but must be subclassed.
L 3.5
Construction
The parent class is used only for its behavior, the child class has no is-a relationship to the parent. Child modify the arguments or names of methods An example might be subclassing the idea of a Set from an existing List class. Child class is not a more specialized form of parent class; no substitutability
L 4.2
Generalization or Extension
The child class generalizes or extends the parent class by providing more functionality In some sense, opposite of subclassing for specialization The child doesn't change anything inherited from the parent, it simply adds new features Often used when we cannot modify existing base parent class Example, ColoredWindow inheriting from Window Add additional data fields Override window display methods
L 4.4
Limitation
The child class limits some of the behavior of the parent class. Example, you have an existing List data type, and you want a Stack Inherit from List, but override the methods that allow access to elements other than top so as to produce errors.
L 4.6
Combimnation
Two or more classes that seem to be related, but its not clear who should be the parent and who should be the child. Example: Mouse and TouchPad and JoyStick Better solution, abstract out common parts to new parent class, and use subclassing for specialization.
L 4.8
Specialization. The child class is a special case of the parent class; in other words, the child class is a subtype of the parent class. Specification. The parent class defines behavior that is implemented in the child class but not in the parent class. Construction. The child class makes use of the behavior provided by the parent class, but is not a subtype of the parent class. Generalization. The child class modifies or overrides some of the methods of the parent class. Extension. The child class adds new functionality to the parent class, but does not change any inherited behavior. Limitation. The child class restricts the use of some of the behavior inherited from the parent class. Variance. The child class and parent class are variants of each other, and the class-subclass relationship is arbitrary. Combination. The child class inherits features from more than one parent class. This is multiple inheritance and will be the subject of a later chapter.
L 4.9
Software Reusability (among projects) Increased Reliability (resulting from reuse and sharing of well-tested code) Code Sharing (within a project) Consistency of Interface (among related objects) Software Components Rapid Prototyping (quickly assemble from pre-existing components) Polymorphism and Frameworks (high-level reusable components) Information Hiding
L 5.1
Execution Speed Program Size Message-Passing Overhead Program Complexity (in overuse of inheritance)
L 5.2
Types of inheritance
Acquiring the properties of an existing Object into newly creating Object to overcome the redeclaration of properties in deferent classes. These are 3 types: 1.Simple Inheritance
SUPER extends
SUB
L 5.3
3. Multiple Inheritance
SUPER 2
implements
SUPER 1 SUPER 2
Visibility modifiers determine which class members are accessible and which do not Members (variables and methods) declared with public visibility are accessible, and those with private visibility are not Problem: How to make class/instance variables visible only to its subclasses? Solution: Java provides a third visibility modifier that helps in inheritance situations: protected
L 6.1
The protected visibility modifier allows a member of a base class to be accessed in the child protected visibility provides more encapsulation than public does protected visibility is not as tightly encapsulated as private visibility
Book protected int pages + getPages() : int + setPages(): void Dictionary + getDefinitions() : int + setDefinitions(): void + computeRatios() : double
L 6.3
Example: Super-Class
class A { int i; void showi() { System.out.println("i: " + i); } }
L 6.4
Example: Sub-Class
class B extends A { int j; void showj() { System.out.println(j: " + j); } void sum() { System.out.println("i+j: " + (i+j)); } }
L 6.5
L 6.9
vol = ship2.volume(); System.out.println("Volume of ship2 is " + vol); System.out.print("Weight of ship2 is ); System.out.println(ship2.weight); System.out.print("Shipping cost: $); System.out.println(ship2.cost); } }
L 6.10
super uses
super is a keyword used to refer to hidden variables of super class from sub class. super.a=a; It is used to call a constructor of super class from constructor of sub class which should be first statement. super(a,b); It is used to call a super class method from sub class method to avoid redundancy of code super.addNumbers(a, b);
L 6.11
Why is super needed to access super-class members? When a sub-class declares the variables or methods with the same names and types as its super-class:
class A { int i = 1; } class B extends A { int i = 2; System.out.println(i is + i); }
L 6.12
Although the i variable in B hides the i variable in A, super allows access to the hidden variable of the super-class:
class UseSuper { public static void main(String args[]) { B subOb = new B(1, 2); subOb.show(); } }
L 6.14
final keyword is used declare constants which can not change its value of definition. final Variables can not change its value. final Methods can not be Overridden or Over Loaded final Classes can not be extended or inherited
L 6.15
A class declared final cannot be inherited has no sub-classes. final class A { } This class declaration is considered illegal: class B extends A { } Declaring a class final implicitly declares all its methods final. It is illegal to declare a class as both abstract and final.
L 6.17
Polymorphism
Polymorphism is one of three pillars of object-orientation. Polymorphism: many different (poly) forms of objects that share a common interface respond differently when a method of that interface is invoked: 1) a super-class defines the common interface 2) sub-classes have to follow this interface (inheritance), but are also permitted to provide their own implementations (overriding) A sub-class provides a specialized behaviors relying on the common elements defined by its super-class.
L 7.1
Polymorphism
A polymorphic reference can refer to different types of objects at different times In java every reference can be polymorphic except of references to base types and final classes. It is the type of the object being referenced, not the reference type, that determines which method is invoked Polymorphic references are therefore resolved at run-time, not during compilation; this is called dynamic binding Careful use of polymorphic references can lead to elegant, robust software designs
L 7.2
Method Overriding
When a method of a sub-class has the same name and type as a method of the super-class, we say that this method is overridden. When an overridden method is called from within the sub-class: 1) it will always refer to the subclass method 2) super-class method is hidden
L 7.3
L 7.5
When show() is invoked on an object of type B, the version of show() defined in B is used:
class Override { public static void main(String args[]) { B subOb = new B(1, 2, 3); subOb.show(); } }
Overloading deals with multiple methods in the same class with the same name but different signatures Overloading lets you define a similar operation in different ways for different data
Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature Overriding lets you define a similar operation in different ways for different object types
L 7.7
Abstract Classes
use the modifier abstract on a class header to declare an abstract class abstract class Vehicle { }
Car
Boat
L 7.8
Plane
Abstract methods consist of only methods declarations, without any method body
public abstract class Vehicle { String name; public String getName() { return name; } \\ method body abstract public void move(); \\ no body! }
L 7.9
Abstract Classes
Abstract methods consist of only methods declarations, without any method body
The non-abstract child of an abstract class must override the abstract methods of the parent An abstract class cannot be instantiated The use of abstract classes is a design decision; it helps us establish common elements in a class that is too general to instantiate
L 7.10
Abstract Method
Inheritance allows a sub-class to override the methods of its super-class. A super-class may altogether leave the implementation details of a method and declare such a method abstract: abstract type name(parameter-list); Two kinds of methods: 1) concrete may be overridden by subclasses 2) abstract must be overridden by subclasses It is illegal to define abstract constructors or static methods.
L 7.11