0% found this document useful (0 votes)
23 views

SE101_Lec4_InheritanceAbstractFinal

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

SE101_Lec4_InheritanceAbstractFinal

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Inheritance

Interface

Multiple

Programming in
Inheritance

Java
OOP Concepts Revisited
Java Basics

Variables/ Instance variables / Constants

Methods

Classes and Objects

DONE…  Constructors

Encapsulation
Inheritance

Interface

Today…  Multiple Inheritance


Inheritance

Interface

Multiple Inheritance
 Inheritance can be defined as the process where
one object acquires the properties of another.

 Excepting Object class, which has no superclass,


Inheritance
every class has one and only one direct
superclass.

 In the absence of any other explicit superclass,


every class is implicitly a subclass of Object.

 The idea of inheritance is simple but powerful: It


allows re-usability.
 A subclass inherits all the members from its
superclass.
 fields
 methods
Key  nested classes
Points:
 Constructors are not members, so they are not
inherited by subclasses, but the constructor of the
superclass can be invoked from the subclass.

 We use ‘extends’ and ‘implements’ keywords to


achieve inheritance.
Super Class:-
 Super classes contains all the attributes & behaviors that are common between classes.

 Consider the following classes:

Student Lecturer Person

name name name


city city city
department course age
age age
tellName()
attendLectures() doLectures() tellAge()
doAssignment() doMarking() tellCity()
doExams() tellName()
tellName() tellAge()
tellAge() tellCity()
tellCity()
class SuperB {
int x;
void setIt (int n) { x=n;}
void increase () { x=x+1;}
void triple () {x=x*3;};
Sub Super int returnIt () {return x;}
Relationsh }
ip class SubC extends SuperB {
void triple () {x=x+3;} // override existing
method
void quadruple () {x=x*4;} // new method
}
public class TestInheritance {
public static void main(String[] args) {

SuperB b = new SuperB();


b.setIt(2);
b.increase();
b.triple();
Example System.out.println( b.returnIt() );

SubC c = new SubC();


c.setIt(2);
c.increase();
c.triple();
System.out.println( c.returnIt() ); }
}
 IS-A is a way of saying : This object is a type of that
object.

public class Animal{


IS-A }

Relationsh public class Mammal extends Animal{


ip: }

public class Reptile extends Animal{


}

public class Dog extends Mammal{


}
In OO Terms:

Animal is the superclass of Mammal class.

Animal is the superclass of Reptile class.


In OO
Terms.. Mammal and Reptile are sub classes of
Animal class.

Dog is the subclass of both Mammal and


Animal classes.
In Is-A Terms:

Mammal IS-A Animal

Reptile IS-A Animal


Is-A Terms
Dog IS-A Mammal

Hence : Dog IS-A Animal as well


class Animal{

public void move(){


System.out.println("Animals can move");
}
}

class Dog extends Animal{

public void move(){


Example }
System.out.println("Dogs can walk and run");

}
public class TestDog{

public static void main(String args[]){


Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object

a.move();// runs the method in Animal class


b.move();//Runs the method in Dog class
}
}
Java supports single inheritance, meaning that
a derived class can have only one parent class
Multiple inheritance allows a class to be
derived from two or more classes, inheriting the
Multiple members of all parents
Inheritance Java does not support multiple inheritance
In most cases, the use of interfaces gives us
aspects of multiple inheritance without the
overhead
Interfaces

Design Abstraction and a way


for loosing realizing Multiple
Inheritance

15
 Interface is a conceptual entity
similar to a Abstract class.
 Can contain only constants (final
variables) and abstract method (no
implementation) - Different from
Interfaces Abstract classes.
 Use when a number of classes share
a common interface.
 Each class should implement the
interface.

16
Interface  An interface is basically a kind of class, but
they have to be only abstract classes and final
s: An fields/variables.
informal
way of  Therefore, it is the responsibility of the class
that implements an interface to supply the
realizing code for methods.
multiple
inheritan  A class can implement any number of
ce interfaces, but cannot extend more than one
class at a time.
<<Interface>>
Speaker
Interface - speak()
Example

Politician Priest Lecturer


speak() speak() speak()

18
interface InterfaceName {
// Constant/Final Variable Declaration

// Methods Declaration – no method body


}
Interfaces
Definition
interface Speaker {
public void speak( );
}
 Interfaces are used like super-classes who properties are
inherited by classes.
 This is achieved by creating a class that implements the
given interface as follows:
Implement
ing
Interfaces class ClassName implements InterfaceName [, InterfaceName2, …]
{
// Body of Class
}
class Politician implements Speaker {
public void speak(){
System.out.println(“Talk politics”);
}
}

Implement
class Priest implements Speaker {
ing public void speak(){
Interfaces }
System.out.println(“Religious Talks”);

Example }

class Lecturer implements Speaker {


public void speak(){
System.out.println(“Talks Object Oriented Design and
Programming!”);
}
}
 Like classes, interfaces can also be
extended. The new sub-interface will
inherit all the members of the
Extending superinterface in the manner similar
Interfaces to classes. This is achieved by using
the keyword extends as follows:

interface InterfaceName2 extends InterfaceName1 {


// Body of InterfaceName2
}
 A general form of interface implementation:

class ClassName extends SuperClass implements InterfaceName [,


InterfaceName2, …]
Inheritance {
// Body of Class
and Interface }

Implementati  This shows a class can extended another


on class while implementing one or more
interfaces.
 It appears like a multiple inheritance (if we
consider interfaces as special kind of classes
with certain restrictions or special features).
 Interfaces, like abstract classes and
methods, provide templates of behaviour
that other classes are expected to
implement.
Interface  Separates out a design hierarchy from
s and implementation hierarchy. This allows
software designers to enforce/pass
Software common/standard syntax for programmers
implementing different classes.
Engineeri  Pass method descriptions, not
ng implementation
 Java allows for inheritance from only a single
superclass. Interfaces allow for class mixing.
 Classes implement interfaces.

24
Final and Abstract Classes

25
Parent

Restricting
Inheritance Inherited
capability

Child

26
Final
Members
 All methods and variables can be
: A way overridden by default in subclasses.
for  This can be prevented by declaring
Preventin them as final using the keyword “final”
g as a modifier. For example:
 final int marks = 100;
Overridin  final void display();
g of  This ensures that functionality defined
Members in this method cannot be altered any.
Similarly, the value of a final variable
in cannot be altered.
Subclass
es 27
 We can prevent an inheritance of classes by
other classes by declaring them as final
Final classes.
Classes:  This is achieved in Java by using the keyword
final as follows:
A way for final class Marks
Preventin { // members
}
g Classes final class Student extends Person

being { // members
}
extended  Any attempt to inherit these classes will
cause an error.

28
 When we define a class to be “final”, it cannot be
extended.
 In certain situation, we want to properties of
classes to be always extended and used. Such
classes are called Abstract Classes.
Abstract
Classes  An Abstract class is a conceptual class.
 An Abstract class cannot be instantiated – objects
cannot be created.

 Abstract classes provides a common root for a


group of classes, nicely tied together in a
package:
29
abstract class ClassName
{
...

abstract Type MethodName1();


Abstract Type Method2()
{
Class }
// method body

Syntax }
 When a class contains one or more abstract methods,
it should be declared as abstract class.
 The abstract methods of an abstract class must be
defined in its subclass.
 We cannot declare abstract constructors or abstract
static methods.

30
Abstract Class - Shape is a abstract
Example class.

Shape

Circle Rectangle

31
public abstract class Shape {
public abstract double area();
public void move() { // non-abstract method
// implementation
The Shape }
Abstract }
Class

 Is the following statement valid?


 Shape s = new Shape();

32
public Circle extends Shape {
protected double r;
protected static final double PI =3.1415926535;
public Circle() { r = 1.0; )
public double area() { return PI * r * r; }
Abstract …
}
Classes public Rectangle extends Shape {
protected double w, h;
public Rectangle() { w = 0.0; h=0.0; }
public double area() { return w * h; }
}

33
 A class with one or more abstract
methods is automatically abstract and
it cannot be instantiated.
 A class declared abstract, even with
no abstract methods can not be
Abstract instantiated.
Classes  A subclass of an abstract class can be
Properties instantiated if it overrides all abstract
methods by implementation them.
 A subclass that does not implement all
of the superclass abstract methods is
itself abstract; and it cannot be
instantiated.

34
 If you do not want (properties of) your class
to be extended or inherited by other classes,
define it as a final class.
 Java supports this is through the keyword “final”.
 This is applied to classes.
 You can also apply the final to only methods
if you do not want anyone to override them.
 If you want your class (properties/methods)
Summary to be extended by all those who want to use,
then define it as an abstract class or define
one or more of its methods as abstract
methods.
 Java supports this is through the keyword
“abstract”.
 This is applied to methods only.
 Subclasses should implement abstract methods;
otherwise, they cannot be instantiated.

35
NEXT CLASS:

Practical Session:
THANK
YOU

You might also like