03: Inherit Bank Account Class as Saving & CurrentAIM            : A class derived from Account that holds information abo...
04: Inherit Bank Account Class as Saving & Current               }}               class SimpleInheritance {               ...
04: Inherit Bank Account Class as Saving & CurrentAs you can see, the subclass B includes all of the members of its superc...
04: Inherit Bank Account Class as Saving & Currentdouble weight; // weight of box// initialize width, height, and depth us...
04: Inherit Bank Account Class as Saving & CurrentAn instance method in a subclass cannot override a static method in the ...
Sdtl assignment 03
Upcoming SlideShare
Loading in …5
×

Sdtl assignment 03

323 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
323
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sdtl assignment 03

  1. 1. 03: Inherit Bank Account Class as Saving & CurrentAIM : A class derived from Account that holds information about saving and currentaccount also implements overrides method and print summary of saving and current account.OBJECTIVE : To expose the concepts of inheritance and overriding.THEORY Inheritance: -Inheritance is one of the cornerstones of object-oriented programmingbecause it allows the creation of hierarchical classifications. Using inheritance, you can create ageneral class that defines traits common to a set of related items. This class can then be inheritedby other, more specific classes, each adding those things that are unique to it. In the terminologyof Java, a class that is inherited is called a superclass. The class that does the inheriting is called asubclass. Therefore, a subclass is a specialized version of a superclass. It inherits all of theinstance variables and methods defined by the superclass.Inheritance Basics: - To inherit a class, you simply incorporate the definition of one classinto another by using the ‘ extends’ keyword. To see how, let’s begin with a short example. Thefollowing program creates a superclass called A and a subclass called B. Notice how the keywordextends is used to create a subclass of A. // A simple example of inheritance. // Create a superclass. class A { int i, j; void showij() { System.out.println("i and j: " + i + " " + j); }} // Create a subclass by extending class A. class B extends A { int k; void showk() { System.out.println("k: " + k);} void sum() { System.out.println("i+j+k: " + (i+j+k)); 17SDTL
  2. 2. 04: Inherit Bank Account Class as Saving & Current }} class SimpleInheritance { public static void main(String args[]) { A superOb = new A(); B subOb = new B(); // The superclass may be used by itself. superOb.i = 10; superOb.j = 20; System.out.println("Contents of superOb: "); superOb.showij(); System.out.println(); /* The subclass has access to all public members of its superclass. */ subOb.i = 7; subOb.j = 8; subOb.k = 9; System.out.println("Contents of subOb: "); subOb.showij(); subOb.showk(); System.out.println(); System.out.println("Sum of i, j and k in subOb:"); subOb.sum(); }} The output from this program is shown here: Contents of superOb: i and j: 10 20 Contents of subOb: i and j: 7 8 k: 9 Sum of i, j and k in subOb: i+j+k: 24 18SDTL
  3. 3. 04: Inherit Bank Account Class as Saving & CurrentAs you can see, the subclass B includes all of the members of its superclass, A. This is whysubOb can access i and j and call showij( ). Also, inside sum( ), i and j can be referred to directly,as if they were part of B. Even though A is a superclass for B, it is also a completely independent,stand-alone class. Being a superclass for a subclass does not mean that the superclass cannot beused by itself. Further, a subclass can be a superclass for another subclass.The general form of a class declaration that inherits a superclass is shown here: class subclass-name extends superclass-name { // body of class }You can only specify one superclass for any subclass that you create. Java does not support theinheritance of multiple superclasses into a single subclass. (This differs from C++, in which youcan inherit multiple base classes.) You can, as stated, create a hierarchy of inheritance in which asubclass becomes a superclass of another subclass. However, no class can be a superclass of itself .Using super:-Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keywordsuper .super has two general forms. The first calls the superclass’ constructor. The second is usedto access a member of the superclass that has been hidden by a member of a subclass. Each use isexamined here.Using super to Call Superclass ConstructorsA subclass can call a constructor method defined by its superclass by use of thefollowing form of super: super(parameter-list);A parameter-list specifies any parameters needed by the constructor in the superclass super( )must always be the first statement executed inside asubclass’ constructor. To see how super( ) is used, consider this example// BoxWeight now uses super to initialize its Box attributes.class BoxWeight extends Box { 19SDTL
  4. 4. 04: Inherit Bank Account Class as Saving & Currentdouble weight; // weight of box// initialize width, height, and depth using super()BoxWeight(double w, double h, double d, double m) {super(w, h, d); // call superclass constructorweight = m;}}Here, BoxWeight( ) calls super( ) with the parameters w, h, and d. This causes the Box( )constructor to be called, which initializes width, height, and depth using these values.BoxWeight no longer initializes these values itself. It only needs to initialize the value unique toit: weight. This leaves Box free to make these values private if desired. In the preceding example,super( ) was called with three arguments. Since constructors can be overloaded, super( ) can becalled using any form defined by the superclass. The constructor executed will be the one thatmatches the arguments. For example, here is a complete implementation of BoxWeight thatprovides constructors for the various ways that a box can be constructed. In each case, super( ) iscalled using the appropriate arguments. Notice that width, height, and depth have been madeprivate within Box.Overriding and Hiding MembersUnder certain circumstances, a subclass may override non-static methods defined in the superclassthat would otherwise be inherited. When the method is invoked on an object of the subclass, it isthe new method implementation in the subclass that is executed. The overridden method in thesuperclass is not inherited by the subclass, and the new method in the subclass must uphold thefollowing rules of method overriding: The new method definition must have the same method signature (i.e., method name and parameters) and the same return type. Whether parameters in the overriding method should be final is at the discretion of the subclass. A methods signature does not encompass the final modifier of parameters, only their types and order. The new method definition cannot narrow the accessibility of the method, but it can widen it. The new method definition can only specify all or none, or a subset of the exception classes (including their subclasses) specified in the throws clause of the overridden method in the super class. 20SDTL
  5. 5. 04: Inherit Bank Account Class as Saving & CurrentAn instance method in a subclass cannot override a static method in the superclass. The compiler willflag this as an error. A static method is class-specific and not part of any object, while overridingmethods are invoked on behalf of objects of the subclass. However, a static method in a subclass canhide a static method in the superclass.A final method cannot be overridden because the modifier final prevents method overriding. Anattempt to override a final method will result in a compile-time error. However, an abstract methodrequires the non-abstract subclasses to override the method, in order to provide an implementation.Accessibility modifier private for a method means that the method is not accessible outside the classin which it is defined; therefore, a subclass cannot override it. However, a subclass can give its owndefinition of such a method, which may have the same signature as the method in its superclass.Overriding vs. OverloadingMethod overriding should not be confused with method overloading. Method overriding requires thesame method signature (name and parameters) and the same return type. Only non-final instancemethods in the superclass that are directly accessible from the subclass are eligible for overriding.Overloading occurs when the method names are the same, but the parameter lists differ. Therefore, tooverload methods, the parameters must differ in type, order, or number. As the return type is not apart of the signature, having different return types is not enough to overload methods.FAQS: 1. Can an inner class declared inside of method access local variables of this method? 2. Whats the main difference between a Vector and an ArrayList? 3. You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces? 4. What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it? 5. When you declare a method as abstract method? 6. Can I call a abstract method from a non abstract method 7. What is the difference between an Abstract class and Interface in Java ? or can you explain when you use Abstract classes ? 8. What is the purpose of garbage collection in Java, and when is it used? 9. What is the purpose of finalization? 10. What is the difference between static and non-static variables? 11. How are this() and super() used with constructors? 12. What are some alternatives to inheritance?

×