Java Methods

Download Report

Transcript Java Methods

JAVA WORKSHOP
SESSION – 3
PRESENTED BY
JAYA RAO MTech(CSE)
NEWTON’S INSTITUTE OF ENGINEERING
1
Session-III
You learn
Inheritance
Constructors
Method overloading
Method overrding
Keywords extends, super,final
Abstract classes
Dynamic method despatch
Polymorphism

2

OOPs revolve around the three concepts:
1. Encapsulation
2. Polymorphism
3. Inheritance
3
The three principles of
OOP

Encapsulation
– Objects hide their functions
(methods) and data
(instance variables)

Inheritance
– Each subclass inherits all
variables of its superclass

car
Polymorphism
– Interface same despite
different data types
manual
draw()
Super class
automatic
Subclasses
draw()
Inheritance


A class can extend another class, inheriting all its
data members and methods while redefining some of
them and/or adding its own.
Inheritance implements the “
between objects.
is a” relationship
5




Using inheritance, you can create a
general class that defines traits common to a set of
related items.
This class can then be inherited by other, more
specific classes, each adding those things that are
unique to it.
In the terminology of Java, a class that is inherited is
called a superclass.
The class that does the inheriting is called a subclass.
6
7

Inheritance implements the “is a” relationship.

Not to be confused with embedding (an object has
another object as a part), which represents the “has
a” relationship:
A sailboat is a boat
A sailboat has a sail
8




Using inheritance, you can create a
general class that defines traits common to a set of
related items.
This class can then be inherited by other, more
specific classes, each adding those things that are
unique to it.
In the terminology of Java, a class that is inherited
is called a superclass.
The class that does the inheriting is called a
subclass.
9


Therefore, a subclass is a specialized
version of a superclass.
It inherits all of the instance variables and
methods defined by the superclass and adds
its own, unique elements.
10
The new class will be similar to the existing
class, but will have some new characteristics.


11
BENEFITS





Inheritance promotes the concept of code
reusability.
Inheritance allows
a better data analysis,
reduction in development time,
less coding and better performance
12
Inheritance (cont’d)
subclass
or
derived class
superclass
extends
or
base class
13


In Java, a subclass can extend only one superclass.
In Java, a class can implement several interfaces —
this is Java’s form of multiple inheritance.
14
Inheritance (cont’d)
Game
Solitaire
GameFor2
BoardGame
Chess
Backgammon
15
extends keyword

The general form
class subclass-name extends superclass-name
{
// body of class
}
16

The following program creates a

superclass called A

subclass called B.
and a
17
class A {
----------;
---------;
}
Class B extends class A {
---------------;
---------------;
}
//super class
//sub class
class mainclass {
public static void main(String args[]) {
}
18


















// A simple example of inheritance.
// Create a superclass.
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);
}
void sum() {
System.out.println("i+j+k: " + (i+j+k));
}
19
}




class SimpleInheritance {
public static void main(String args[]) {
A superOb = new A();
B subOb = new B();
subOb.showij();
subOb.showk();


System.out.println();

System.out.println("Sum of i, j and k in subOb:");

subOb.sum();

}


}
20
Inheritance (cont’d)

An object of a class at the bottom of a
hierarchy inherits all the methods of all the
classes above.
21




Simple Single Inheritance
The class that is used as a basis for defining a
new class is called a parent class (or superclass or
base class.)
•The new class based on the parent class is called
a child class (or subclass or derived class.)
• In Java, children inherit characteristics from just
one parent.

This is called single inheritance.
22
Single inheritance :
A derived class with only one base
class is called single inheritance.
23
Multiple inheritance

A derived class with several base
classes is called multiple inheritance
24
25
Multilevel inheritance

The mechanism of deriving a class
from another derived class is called
multilevel inheritance.
26
27
Hierarchical inheritance


One class may be inherited by more
than one classes.
This process is known as hierarchical
inheritance
28
29
Hybrid inheritance

It is a combination of hierarchical and
multiple inheritance.
30
31
32
Using super



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.
33
Using super to Call
Superclass Constructors



A subclass can call a constructor method
defined by its superclass by use of the
following form of super:
super(parameter-list);
super( ) must always be the first statement executed inside
a subclass’ constructor.
34


parameter-list is defined by the constructor in
the superclass.
super(parameter-list) must be the first
statement executed inside a subclass'
constructor.
35


Here is a demo for how to use super to call
constructor from parent class
See pgm shapessuper1.java
36
Use super to reference members
from parent class

Its general form is:

super.member


member can be either a method or an
instance variable.
See pgm super2.java
37
When Constructors Are
Called



In a class hierarchy, constructors are called
in order of derivation, from superclass to
subclass.
The following program illustrates when
constructors are executed:
See constructorder.java
38
What is Method Overriding


Method Overriding happens when a method in a subclass
has the same name and type signature as a method in its
superclass.
When an overridden method is called within a subclass, it
will refer to the method defined in the subclass.

The method defined by the superclass will be hidden.

See pgm override1.java or shaperssuper2.java
39
super and overridden method


To access the superclass version of an overridden
function, you can do so by using super.
See override2.java or shapessuper2.java
40
Method overriding vs method
overload




Method overriding occurs when the names and
the type signatures of the two methods are
identical.
If not, the two methods are overloaded.
For example, consider this modified version of the
preceding example:
See prg override3.java
41
Dynamic Method Dispatch



When an overridden method is called through a
superclass reference, Java determines which
version of that method to execute.
Here is an example that illustrates dynamic method
dispatch:
see dmd.java
42
What are Abstract Classes



You can require that certain methods be
overridden by subclasses by specifying the
abstract type modifier.
To declare an abstract method, use this
general form:
abstract type name(parameter-list);
43


No method body is present for
abstract method.
Any class that contains one or more
abstract methods must also be
declared abstract.
44




abstract class MyAbstractClass{
abstract type name(parameter-list);
}
Here is an abstract class, followed by a class
which implements its abstract method.
See prg abs1.java
45
Using abstract methods
and classes


http://www.java-examples.com/javastring-lower-case-example
http://www.particle.kth.se/~lindsey/Ja
vaCourse/Book/Part1/Java/Chapter02/
exercises.html
46