11
Day 5 Day 5 Inheritance Inheritance Packages Packages

Document2

Embed Size (px)

DESCRIPTION

Hariprasanna V (9843824677)

Citation preview

Page 1: Document2

Day 5Day 5

Inheritance Inheritance PackagesPackages

Page 2: Document2

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.

Page 3: Document2

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 variableMethod 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 method is solved at run-time, rather than compile time. This method is important coz this is how java implements run-time polymorphism.

Page 4: Document2

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.

Page 5: Document2

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.

Page 6: Document2

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.

Page 7: Document2

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.

Page 8: Document2

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); }

Page 9: Document2

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(); }}

Page 10: Document2

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("-->> "); System.out.println(name + ": $" + bal); }}

Page 11: Document2

class AccountBalance { public static void main(String args[]) { Balance current[] = new Balance[3]; current[0] = new Balance("K. J. Fielding", 123.23); current[1] = new Balance("Will Tell", 157.02); current[2] = new Balance("Tom Jackson", -12.33); for(int i=0; i<3; i++) current[i].show(); }}