You are on page 1of 23

Java

Let us start programming (Contd..)

Inheritance
Inheritance is one of the cornerstones of objectoriented programming.
A class that is inherited is called a superclass. The
class that does the inheriting is called a subclass.
A subclass is a specialized version of a superclass.
extends is the keyword used to inherit a class.
The general form of a class declaration :
class subclass-name extends superclass-name {
// body of class
}

Inheritance - Example
class Base
{
int i, j;
void showij()
{
System.out.println("i and j in Base: " + i + " & " + j);
}
}
class Sub extends Base
{
void sum()
{
System.out.println( i+j in Sub: " + (i+j));
}
}

Inheritance - Example
class SimpleInheritance
{
public static void main(String args[])
{
Sub subOb = new Sub();
subOb.i = 10;
subOb.j = 15;
subOb.showij();
subOb.sum();
}
}

super - Keyword
super has two general forms.
The first calls the superclass constructor.
The second is used to access a member of the superclass
that has been hidden by a member of a subclass.

super( ) must always be the first statement executed


inside a subclass constructor.
super always refers to the superclass of the subclass
in which it is used. The general form is,
super.member
Here, member can be either a method or an instance variable.

Method Overriding
In a class hierarchy, when a method in a 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 method in the superclass.
To access the superclass version of an overridden
function, you can do so by using super.
Method overriding occurs only when the names and
the type signatures of the two methods are identical.

Method Overriding - Example


class Base1
{
int i = 10;
void show()
{
System.out.println("i in Base1: " + i);
}
}
class Sub1 extends Base1
{
int j = 15;
void show()
{
System.out.println("j in Sub1: " + j);
}
}

Method Overriding - Example


class Override
{
public static void main(String args[])
{
Sub1 subOb = new Sub1();
subOb.show();
}
}

Dynamic Method Dispatch


Dynamic method dispatch is the mechanism by
which a call to an overridden function is resolved at
run time, rather than compile time.
It is important because this is how Java implements
run-time polymorphism.
A superclass reference variable can refer to a
subclass object.
When an overridden method is called through a
superclass reference, Java determines which version
of that method to execute based upon the type of the
object being referred to at the time the call occurs.

DMD - Example
class X
{
void callme()
{
System.out.println("Inside X's callme method");
}
}
class Y extends X
{
void callme()
{
System.out.println("Inside Y's callme method");
}
}
class Z extends Y
{
void callme()
{
System.out.println("Inside Z's callme method");
}
}

DMD - Example
class Dispatch
{
public static void main(String args[])
{
X x = new X();
Y y = new Y();
Z z = new Z();
X r; // Reference of type X
r = x;
r.callme();
r = y;
r.callme();
r = z;
r.callme();
}
}

Using final with Inheritance


To disallow a method from being overridden,
specify final as a modifier at the start of its
declaration.
Methods declared as final cannot be overridden.
By declaring a class as final, we can prevent a class
from being inherited.
Declaring a class as final implicitly declares all of
its methods as final, too.

Abstract class
To declare an abstract method, the general form is :
abstract type name(parameter-list);

Any class that contains one or more abstract


methods must also be declared abstract.
An abstract class cannot be directly instantiated with
the new operator.
Any subclass of an abstract class must either
implement all of the abstract methods in the
superclass, or be itself declared abstract.

Abstract class - Example


abstract class Aabs
{
abstract void callme();
void callmetoo()
{
System.out.println(This is a concrete method );
}
}
class Bnabs extends Aabs
{
void callme()
{
System.out.println(Bnabs impletation of callme);
}
}

Abstract class - Example


class Abstract
{
public static void main(String args[])
{
Bnabs b = new Bnabs();
b.callme ();
b.callmetoo ();
}
}

Interface
Interfaces are syntactically similar to classes, but
they lack instance variables, and their methods are
all abstract methods.
A class can implement any number of interfaces.
Each class that implements an interface must define
all of the methods declared in that interface.
When you implement an interface method, it must
be declared as public.
Variables declared inside an interface are implicitly
final and static.

Interface - Example
interface InterOne
{
void implementMe();
}
interface InterTwo
{
void implementMeToo();
}

Interface - Example
class Client implements InterOne, InterTwo
{
public void implementMe()
{
System.out.println("Implement One");
}
public void implementMeToo()
{
System.out.println("Implement Two");
}
void nonInterfaceMeth()
{
System.out.println("Client Method");
}
}

Interface - Example
class TestInterface
{
public static void main(String args[])
{
Client clOb = new Client();
clOb.implementMe();
clOb.implementMeToo();
clOb.nonInterfaceMeth();
}
}

Packages
Package is a mechanism for partitioning the class name
space into more manageable chunks.
The package is both a naming and a visibility control
mechanism.
You can define classes inside a package that are not
accessible by code outside that package.
To create a package, include a package command as the
first statement in a Java source file.
package MyPackage;

Java uses file system directories to store packages. For


example, the .class files for any classes you declare to
be part of MyPackage must be stored in a directory
called MyPackage.

Package - Example
package MyPackage;
public class MyPack
{
public int add(int a, int b)
{
System.out.println(My Package's Method);
return a + b;
}
}

Package - Example
import MyPackage.MyPack;
public class TestPack
{
public static void main(String args[])
{
MyPack packOb = new MyPack();
int val = packOb.add(10,10);
System.out.println("Value is " + val);
}
}

Access Specifier
Access Levels

Access Specifier / Modifier


private

No-modifier

protected

public

Same Class

Sub class of same


package

Non Sub class of


same package

Sub class of diff.


package

Non Sub class of


diff. package

You might also like