23 24 LP 21 ISC12 C12 Inheritance, Interface&polymorphism
23 24 LP 21 ISC12 C12 Inheritance, Interface&polymorphism
ISC 12
SECTION C
concept
OBJECTIVES
To understand
class Student
{ int roll, marks;
String name;
void enroll()
{ …..
}
void payFee( )
{ …..
}
void generateReportCard()
{ …..
}
}
Student raju = new Student( );
DATA ABSTRACTION
Act of representing essential features without including the
background details
E.g.: To learn how to drive a car we only need to know how to
use steering, accelerator, clutch ,brake and the gears. We need
NOT know how each of these work or the technology behind
it.
POLYMORPHISM
Ability for a message or data to be processed in more than one
form
This is implemented
using function overloading
FUNCTION OVERLOADING
When more than one functions have the same name but the type
and number of arguments (method signature) are different ,
they are said to be over loaded . Thus, parameter list of each
function should be unique. Function overloading implements
Polymorphism. For e.g.
int sample (int a)
int sample (int a , int c)
int sample (int a, String str)
FUNCTION OVERLOADING
12
TRANSITIVE
Inheritance can be used to define a hierarchy of classes in an application:
Uniform
VSA
PHASE1 PHASE2
ISC
subclass superclass
or extends or
derived class base class
Base Class: It is the class whose properties are inherited by
another class. It is also called Super Class.
Derived Class: It is the class that inherit properties from base
class. It is also called Sub Class.
EXAMPLE
GENERAL SYNTAX
class baseclass
{
public
private
protected
}
INHERITANCE IN JAVA
Inheritance is declared using the "extends" keyword
class Person
{ Person
String name; - name: String
Date dob; - dob: Date
[...]
}
superclass: Person
- name: String
The subclass can: - dob: Date
Add new functionality
Use inherited functionality
Override inherited functionality
subclass:
Employee
- employeeID: int
- salary: int
- doj: Date
EXAMPLE -INHERITANCE
class Calculation public class My_Calculation extends Calculation
{
{ public void multiplication(int x, int y)
int z; {
z = x * y;
public void addition(int x, int y) System.out.println("The productis :"+z);
{ }
z = x + y; public static void main()
System.out.println("The sum is:"+z); {
int a = 20, b = 10;
}
My_Calculation demo = new My_Calculation();
public void Subtraction(int x, int y) demo.addition(a, b);
{ demo.Subtraction(a, b);
demo.multiplication(a, b);
z = x - y; }
System.out.println("The difference }
is:"+z);
}
} ASSIGNMENT 1-3
ASSIGNMENT 1
ASSIGNMENT 2
TYPES OF INHERITANCE
SUBCLASS NON-SUBCLASS
PRIVATE YES NO NO NO NO
Ex2.Inheritance Practice
ASSIGNMENT 3
ASSIGNMENT 4
EXAMPLE3 PROBLEM USING CONSTRUCTORS
A class Employee contains employee details and another class Retire calculates the employee’s Provident Fund and
Gratuity. The details of the two classes are given below:
Class name : Employee
Data Members:
Name : stores the employee name
ID : stores the employee identification number
basicPay : stores the employee basic monthly salary (in decimals)
accNum : stores the employee bank account number
Member functions:
Employee ( …. ) : parameterized constructor to assign value to data members
void display( ) : to display the employee details
In the below Images, you can see, Man is only one, but he takes multiple
roles like - he is a dad to his child, he is an employee, a salesperson and
many more. This is known as Polymorphism.
42
EXAMPLE-OVERRIDING
// program to implement overriding with constructor inheritance
import java.util.*; class salary extends pay
class pay {
{ double da,hra,gross;
String name;
double basic; void calculate()
Scanner sc=new Scanner(System.in); {
void getdata() getdata();
{ da=.2*basic;
System.out.println("Enter name and basic"); hra=.15*basic;
name=sc.next(); gross=basic+da+hra;
basic=sc.nextDouble(); }
} void display()
void display() {
{ System.out.println("da"+da+"hra"+hra+"gross"+gross);
System.out.println("name"+name+"basic"+basic); }
} void test()
} {
calculate();
super.display();
display();
}
public static void main()
{
salary obj= new salary();
obj.test(); Ex.overriding-pay2
}
}
ASSIGNMENT 7
ABSTRACT CLASS
45
ABSTRACT CLASSES
• Abstract classes are only used as super classes
• Classes are declared as abstract classes only if they will never
be instantiated(ie.no object can be created)
• Abstract classes contain usually one or more abstract methods
• Example:
public abstract class Mouse
{
…
abstract void makeMove( );
}
ABSTRACT METHODS
Ex. Interface
DIFFERENCES BETWEEN AN INTERFACE AND MULTIPLE
INHERITANCE
}
PROGRAMS-PAST EXAM PAPERS
ASSIGNMENT 10 - ISC2016
ASSIGNMENT 11 - ISC2017
ASSIGNMENT 12 - ISC 2018
ASSIGNMENT 13 - ISC 2019
THANK YOU........