03 AdvancedOOP
03 AdvancedOOP
Version 1.0
20 November 2012
Introduction
Java provides features for all the OOP concepts like: Inheritance Polymorphism Abstraction In addition it provides a mechanism of interface, using which we can hide the details of implementation.
20 November 2012
Inheritance
Inheritance is a mechanism to reuse code. Java uses extends keyword for inheritance. Example: class CR extends Trainee { } A class can inherit only one class at a time. Multiple inheritance is not supported in Java. By inheriting, we get three benefits: Use already available functions Add more functions If necessary, modify the available function (method overriding)
20 November 2012
Method Overriding
A method defined in the base class can be overridden in the derived class. This will change the behavior in the derived class. The signature should be same in both classes.
20 November 2012
20 November 2012
20 November 2012
20 November 2012
20 November 2012
Polymorphism
Polymorphism means, behaving differently at different time depending on the situation. Method overriding is one type of polymorphism. Here the method is invoked based on the object. This type of polymorphism is called dynamic polymorphism. It is also called as late binding because method is attached during run-time. The other type of polymorphism is method overloading, which known as static polymorphism.
20 November 2012
Method Overloading
Java allows to have more than one method in the class to have same name, but different signature. Signature includes method name and parameter types. Example: int add(int a, int b); float add(float a, float b);
20 November 2012
10
20 November 2012
11
Abstract Classes
The classes without complete implementation. The keyword used is abstract. Abstract classes cannot be instantiated. Abstract classes contain zero or more abstract methods, which are later implemented by concrete classes. Abstract classes help in bringing common functionalities in derived classes, but slight change in behaviour.
20 November 2012
12
Car drive(){ }
Bus Drive(){}
We can drive both Car and Bus. But the way of driving is slightly different.
Putting the drive method in an abstract class vehicle guarantees that if it is a vehicle it can be driven.
20 November 2012 13
Interface
An Interface is a named collection of method definitions (without implementations). There are two roles involved in interfaces: Those who define interface They get the benefit that all the classes implementing the interface will have the same method signature.0 Those who implement interface. They have the benefit that, they can be referred to by using the interface name.
20 November 2012
14
Interface Example
Interface Definition:
public interface Programmer{ void doProgramming(); }
Interface Implementation:
public class Trainee implements Programmer { void doProgramming(){ System.out.println(Doing program); } void takeLunch(){ } }
20 November 2012
15
Using interface data type, We can access only methods defined in interface and not other methods.
20 November 2012 16
Interface
Contents of an interface are either constant declarations or method declarations. All the methods defined in interface are by default public and abstract. All constant values defined in an interface are implicitly public, static and final.
20 November 2012
17
20 November 2012
18
20 November 2012
19
Summary
In this session you learned: Method Overloading Inheritance Method Overriding Abstract Classes Interfaces
20 November 2012
20