Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 20

OOP in Java

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

Rules for overriding


A subclass CANNOT override methods that are declared final. A subclass MUST override methods that are declared abstract.

20 November 2012

The super Keyword


super is a Java keyword that allows a method to access / refer the hidden variables and overridden methods of the super class. Example: super.aNumber; super.getNumber(); The method super() is used to invoke the constructor of super class.

20 November 2012

The Cosmic Super Class


The Object class sits at the top of the class hierarchy tree. By default, every class in the Java system is descendent of the Object class. This class defines the basic state and behavior which all classed must possess.

20 November 2012

The Cosmic Super Class


The minimal behavior of all java objects are: to compare oneself with another object to provide a string representation to itself. to wait on a condition variable to notify other objects that a condition variable has changes to return the class of the object Object class provides methods for all the above functionalities.

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

Overloading Vs. Overriding


Overloading means the method name is same but the signature is different. Applicable only within a class. Overriding means the name and signature are same but behavior may be different. Applicable in class hierarchies.

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

Abstract Class Example


<<abstract>> Vehicle abstract drive()

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

Interface Example (Continued).


Valid way of creating objects:
Trainee t = new Trainee(); Programmer p = new Trainee();
Note that in the second case, we refer to a Trainee object by using interface data type. t.doProgramming(); p.doProgramming(); t.takeLunch(); p.takeLunch(); // right // right // right // wrong

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

Interfaces Vs. Abstract Classes


Interfaces dont worry about class relationships. It just requires that the classes should implement them. Abstract classes force strict class relationships.

20 November 2012

18

Interfaces Vs. Multiple Inheritance


Interfaces are not synonymous with multiple inheritance. However, a class can implement more than one interface. So in a way, we can tell interface is substitute for multiple inheritance.

20 November 2012

19

Summary
In this session you learned: Method Overloading Inheritance Method Overriding Abstract Classes Interfaces

20 November 2012

20

You might also like