Upcoming SlideShare
Loading in...5




Hariprasanna V (9843824677)

Hariprasanna V (9843824677)



Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

3 3 Presentation Transcript

  • Day 5
    • Inheritance
    • Packages
  • 3 Inheritance
    • Inheritance is the process by which one object acquires the properties of another object.
    • A class that is inherited is called a superclass.
    • The class that does the inheriting is called the subclass.
    • It cannot access those members of the superclass that have been declared as private.
    • super : Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super.
    • 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.
    • A subclass can call a constructor defined by its superclass by use of the following form of the super:
    • super(parameter.list)
    • The second form of the super is :
    • super.member // here member can be either a method or an instance variable
    • 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.
    • 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.
    • Abstract Classes : To declare an abstract method, use the general form:
    • abstract type-name(parameter-list);
    • 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.
    • Any class that contains one or more abstract methods must also be declared abstract.
    • Its not possible to declare abstract constructors, or abstract static methods.
    • Any subclass of an abstract class must either implement all of the abstract methods in the superclass, or be itself declared abstract.
    • Using final with Inheritance
    • To prevent Overriding & To prevent Inheritance.
  • 3.1 Packages
    • Packages are containers for classes that are used to keep the class name space compartmentalized.
    • 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.
    • 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.
    • 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.
    • The general form of the package is:
    • package pkg; // here, pkg is the name of the
    • package.
    • Java uses file system directories to store packages.
    • Importing packages.
  • Sample Code Inheritance: 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)); } } class SimpleInheritance { public static void main(String args[]) { B subOb = new B(); 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(); } }
  • 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); } }
  • 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(); } }