Published on

Hariprasanna V (9843824677)

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Day 5 <ul><li>Inheritance </li></ul><ul><li>Packages </li></ul>
  2. 2. 3 Inheritance <ul><li>Inheritance is the process by which one object acquires the properties of another object. </li></ul><ul><li>A class that is inherited is called a superclass. </li></ul><ul><li>The class that does the inheriting is called the subclass. </li></ul><ul><li>It cannot access those members of the superclass that have been declared as private. </li></ul><ul><li>super : Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super. </li></ul><ul><li>super has two general forms. The first calls the superclass constructors. The second is used to access a member of the superclass that has been hidden by a member of the subclass. </li></ul><ul><li> </li></ul>
  3. 3. <ul><li>A subclass can call a constructor defined by its superclass by use of the following form of the super: </li></ul><ul><li>super(parameter.list) </li></ul><ul><li>The second form of the super is : </li></ul><ul><li>super.member // here member can be either a method or an instance variable </li></ul><ul><li>Method Overriding : When a method in the subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the second method in the superclass. </li></ul><ul><li>Dynamic Method Dispatch : It is the mechanism by which a call to an overridden metod is solved at run-time, rather than compile time. This method is important coz this is how java implements run-time polymorphism. </li></ul>
  4. 4. <ul><li>Abstract Classes : To declare an abstract method, use the general form: </li></ul><ul><li>abstract type-name(parameter-list); </li></ul><ul><li>There can be no objects of an abstract class. That is, an abstract class cannot be directly instantiated with a new operator. Such objects would be useless, coz an abstract class is not fully defined. </li></ul>
  5. 5. <ul><li>Any class that contains one or more abstract methods must also be declared abstract. </li></ul><ul><li>Its not possible to declare abstract constructors, or abstract static methods. </li></ul><ul><li>Any subclass of an abstract class must either implement all of the abstract methods in the superclass, or be itself declared abstract. </li></ul><ul><li>Using final with Inheritance </li></ul><ul><li>To prevent Overriding & To prevent Inheritance. </li></ul>
  6. 6. 3.1 Packages <ul><li>Packages are containers for classes that are used to keep the class name space compartmentalized. </li></ul><ul><li>A package allows you to create a class named List, which you can store in your own package without concern that it will collide with some other named list stored elsewhere. </li></ul><ul><li>To create a package simply include a package command as the first statement in a java source file. Any classes declared within that file will belong to the specified package. </li></ul>
  7. 7. <ul><li>The package statement defines a name space in which classes are stored. If you omit the package statement, the class name are put into the default package, which has no name. </li></ul><ul><li>The general form of the package is: </li></ul><ul><li>package pkg; // here, pkg is the name of the </li></ul><ul><li>package. </li></ul><ul><li>Java uses file system directories to store packages. </li></ul><ul><li>Importing packages. </li></ul>
  8. 8. Sample Code Inheritance: class A { int i, j; void showij() { System.out.println(&quot;i and j: &quot; + i + &quot; &quot; + j); } } // Create a subclass by extending class A. class B extends A { int k; void showk() { System.out.println(&quot;k: &quot; + k); }
  9. 9. void sum() { System.out.println(&quot;i+j+k: &quot; + (i+j+k)); } } class SimpleInheritance { public static void main(String args[]) { B subOb = new B(); subOb.i = 7; subOb.j = 8; subOb.k = 9; System.out.println(&quot;Contents of subOb: &quot;); subOb.showij(); subOb.showk(); System.out.println(); System.out.println(&quot;Sum of i, j and k in subOb:&quot;); subOb.sum(); } }
  10. 10. Package: package MyPack; class Balance { String name; double bal; Balance(String n, double b) { name = n; bal = b; } void show() { if(bal<0) System.out.print(&quot;-->> &quot;); System.out.println(name + &quot;: $&quot; + bal); } }
  11. 11. class AccountBalance { public static void main(String args[]) { Balance current[] = new Balance[3]; current[0] = new Balance(&quot;K. J. Fielding&quot;, 123.23); current[1] = new Balance(&quot;Will Tell&quot;, 157.02); current[2] = new Balance(&quot;Tom Jackson&quot;, -12.33); for(int i=0; i<3; i++) current[i].show(); } }