Вы находитесь на странице: 1из 40

J2SE Core Java

PG-DESD Aug -2013

Session 4:
Learning Objectives
By the end of this session, you must be able to
Describe Inheritance Explain Method Overriding in classes Understand run-time polymorphism Use super keyword Explain Abstract Class Describe Interfaces

Inheritance

Inheritance
INHERITANCE is one of the pillars of object-orientation

A new class is derived from an existing class: 1) Existing class is called super-class 2) Derived class is called sub-class
A sub-class is a specialized version of its super-class: 1) has all non-private members of its super-class 2) may provide its own implementation of super-class methods Objects of a sub-class are a special kind of objects of a superclass

Class Hierarchy
A child class of one parent can be the parent of another child, forming class hierarchies At the top of the hierarchy theres a default class called Object

Animal

Reptile

Bird

Mammal

Snake

Lizard

Parrot

Horse

Bat

Class Object
In Java, all classes use inheritance.

If no parent class is specified explicitly, the base class Object is implicitly inherited.
All classes defined in Java, is a child of Object class, which provides minimal functionality guaranteed to e common to all objects.

Class Object
Methods defined in Object class are; 1. equals(Object obj) Determine whether the argument object is the same as the receiver 2. getClass() Returns the class of the receiver, an object of type Class 3. hashCode() Returns a hash value for this object. Should be overridden when the equals method is changed 4. toString() Converts object into a string value. This method is also often overridden 5. finalize() Called by the garbage collector on an object when garbage collectior determines that there are no more references to the object 6. clone() Creates and returns a copy of this object. 7. wait() 8. wait(long timeout) 9. wait(long timeout, int nanos) 10. notify() 11. notifyAll()

Extends Key Word


It is a keyword used to inherit a class from another class Allows to extend from only one class class One { int a=5; class Two extends One { int b=10; }

One baseobj=new One(); // base class object. Two subobj=new Two(); // child class object super class object baseobj can be used to refer its sub class objects. For example, Baseobj=subobj // now its pointing to sub class

Example

Example
class SuperClass{ void m1(){ System.out.println("Super Class m1"); } } public class SubClass extends SuperClass{ extends SuperClass { void m1(){ System.out.println("Sub Class m1"); } public static void main(String[] args) { SubClass sb=new SubClass(); Overriding the sb.m1(); method SuperClass sp=new SubClass(); sp.m1(); } }

Syntax for Inheriting a class

Reffering a sub class object using super class

The Benefits of Inheritance


Software Reusability (among projects)

Increased Reliability (resulting from reuse and sharing of welltested code)


Code Sharing (within a project) Consistency of Interface (among related objects) Software Components Rapid Prototyping (quickly assemble from pre-existing components)

Polymorphism and Frameworks (high-level reusable components)


Information Hiding

The Costs of Inheritance


Execution Speed Program Size Message-Passing Overhead

Program Complexity (in overuse of inheritance)

1 . Referring immediate parent class instance variable

1. Final variable can not be changed


2. Final method can not be overridden 3. Final class can not be inherited

abstract class - features


If a class contains at least one abstract method should be declared as abstract We can declare a class as abstract, even if the class does not have any abstract methods Abstract class can not be instantiated but can be referred If a class is extending from an abstract class, the extending class should provide the body(implementation) for all the abstract methods of super class If the extended class fails to provide body for at least one abstract method, should be declared abstract

abstract

class MyClass{ void m1(){ System.out.println("in MyClass m1"); } abstract void m2();

This class should be declared abstract because of this abstract method

}
This class can be declared abstract even it does not have any abstract methods

abstract class MyClass{ void m1(){ System.out.println("in MyClass m1"); } void m2(){ System.out.println("in MyClass m1"); } An abstract can also have all abstract methods, but not so }

useful, for this we have a different mechanism called interfaces

Abstract class Examples

This class is having abstract class MyClass{ 2 Abstract abstract void m1(); methods abstract void m2(); void m3(){ System.out.println("in MyClass m3"); } AbstractTest should implement the 2 } abstract methods of MyClass

So we are Implementing the 2 Abstract methods of super class

public class AbstractTest AbstractTest extends extends MyClass{ MyClass{ void m1(){ System.out.println("MyClass-m1 defined by AbstractTest"); } void m2(){ System.out.println("MyClass-m2 defined by AbstractTest"); } public static void main(String[] args) { MyClass mc=new AbstractTest(); mc.m1(); mc.m2(); mc.m3(); } }

Example

Interfaces
An interface is a reference type, similar to a class, that can contain only constants, method signatures, and nested types.

There are no method bodies.


Interfaces cannot be instantiatedthey can only be implemented by classes or extended by other interfaces.

A method declaration within an interface is followed by a semicolon, but no braces.


All methods declared in an interface are implicitly public.

An interface can contain constant declarations in addition to method declarations.


All constant values defined in an interface are implicitly public static final.

Uses of Interfaces

Run time Polymorphism

Interface vs Abstract Class


Abstract Classes
1. Contain one or more abstract methods, can contain no abstract methods also i.e. only concrete methods & can have instance variables. Keyword abstract
Contain private ,protected and public members.

Interfaces
1. Contain only method declarations and public static final constants
Keyword interface Only have public members A class implementing an interface must implement all of the methods defined in the interface

2. 3. 4.

2.
3.

4.

A class extending an abstract class need not implement any of the methods defined in the abstract class

Next

Packages

Access Modifiers
Wrapper Classes