CS233 Module 3-Part 2-1
CS233 Module 3-Part 2-1
25
By: Shamama Anwar
Inheritance terms
• Superclass, base class, parent class: terms to describe the parent in the
relationship, which shares its functionality
• Subclass, derived class, child class: terms to describe the child in the
relationship, which accepts functionality from its parent
26
By: Shamama Anwar
Types of Inheritance
1. Single Inheritance: One class extends only one class.
Person
superclass: - name: String
Base Class - dob: Date
27
By: Shamama Anwar
Types of Inheritance
class Person
{ String name;
Date dob;
[...]
}
class Employee extends Person
{ int employeID;
int salary;
Date doj;
[...]
}
…
…
…
28
By: Shamama Anwar
Types of Inheritance
Person Person
superclass: - name: String
name = “ABC"
- dob: Date dob = Jan 13, 1954
subclass: Employee
- employeeID: int
- salary: int
- doj: Date
Employee
name = “XYZ"
dob = Mar 15, 1968
employeeID = 37518
salary = 65000
doj = Dec 15, 2000
29
By: Shamama Anwar
Types of Inheritance
2. Multilevel Inheritance: Hierarchy of single level inheritance.
Person
Class A superclass: - name: String
- dob: Date
Class B
Employee
subclass: - employeeID: int
- salary: int
Class C
- doj: Date
Contract_Employee
subclass:
- hrs: int
30
By: Shamama Anwar
Types of Inheritance
class Person
{ String name;
Date dob;
[...]
}
class Employee extends Person
{ int employeID;
int salary;
Date doj;
[...]
}
class Contract_Employee extends Employee
{ int hrs;
[...]
}
…
31
By: Shamama Anwar
Types of Inheritance Person
name = “ABC"
Person dob = Jan 13, 1954
superclass: - name: String
- dob: Date
Employee
name = “XYZ"
dob = Mar 15, 1968
employeeID = 37518
subclass: Employee salary = 65000
- employeeID: int
- salary: int doj = Dec 15, 2000
- doj: Date
Contract_Employee
name = “DEF"
Contract_Employee
subclass: - hrs: int dob = Mar 21, 1975
employeeID = 37529
salary = 75000
doj = Dec 15, 2002
hrs = 15
32
By: Shamama Anwar
Types of Inheritance
3. Multiple Inheritance: One class extending from more than one class.
Class A Class B
Class C
33
By: Shamama Anwar
Types of Inheritance
4. Hierarchical Inheritance: More than one class can inherit from a single
class.
Employee
Class A - employeeID: int
- salary: int
- doj: Date
Class B Class C
Teaching Non_Teaching
- semester: int - post: String
- course: String - section: String
34
By: Shamama Anwar
Types of Inheritance
class Employee
{ int employeeid;
int salary;
Date doj;
[...]
}
class Teaching extends Employee
{ int semester;
String course;
[...]
}
class Non_Teaching extends Employee
{ String post;
String section;
[...]
}
… Shamama Anwar
By:
35
Types of Inheritance
Employee Employee
- employeeID: int employeeID = 37518
- salary: int salary = 65000
- doj: Date doj = Dec 15, 2000
Teaching
employeeID = 37518
salary = 65000
Teaching doj = Dec 15, 2000
- semester: int semester: 3
- course: String course: BE
Non_Teaching
Non_Teaching employeeID = 37518
- post: String salary = 65000
- section: String doj = Dec 15, 2000
post: Lab Incharge
section: Lab1
36
By: Shamama Anwar
Types of Inheritance
5. Hybrid Inheritance: Combination of single level, multilevel or hierarchical
inheritance.
Person
- name: String
- dob: Date
Class A
Employee
- employeeID: int
Class B - salary: int
- doj: Date
Class C Class D
Teaching Non_Teaching
- semester: int - post: String
- course: String - section: String
37
By: Shamama Anwar
Types of Inheritance
class Person class Teaching extends Employee
{ String name; { int semester;
Date dob; String course;
[...] [...]
} }
class Employee extends class Non_Teaching extends
Person Employee
{ int employeeid; { String post;
int salary; String section;
Date doj; [...]
[...] }
} …
38
By: Shamama Anwar
Types of Inheritance
Person
Person
- name: String name = “XYZ”
- dob: Date Employee
dob = Dec 15, 2000
name = “ABC”
dob = Mar 21, 1980
employeeID = 37518
Employee salary = 65000
- employeeID: int Teaching doj = Dec 15, 2000
- salary: int name = “ABC”
dob = Mar 21, 1980
- doj: Date
employeeID = 37518
salary = 65000
doj = Dec 15, 2000
semester: 3
course: BE
Non_Teaching
Teaching name = “ABC”
dob = Mar 21, 1980
- semester: int
employeeID = 37518
- course: String
salary = 65000
doj = Dec 15, 2000
Non_Teaching post: Lab Incharge
- post: String section: Lab1
- section: String 39
By: Shamama Anwar
Inheritance: Example 1
class Person
{ String name;
Date dob;
void getdata() { Scanner sc = new Scanner(System.in);
System.out.println(“Enter name:”);
name = sc.next();
System.out.println(“Enter DOB (dd mm yyyy):”);
int dd = sc.nextInt();
int mm = sc.nextInt();
int yy = sc.nextInt();
dob = new Date(yy, mm, dd); }
void display ()
{ System.out.println(“Name:”+name);
System.out.println(“DOB:”+dob);
}
}
40
By: Shamama Anwar
Inheritance: Example 1
class Employee extends Person
{ int employeID;
int salary;
Date doj;
void getdata1() { Scanner sc = new Scanner(System.in);
System.out.println(“Enter employee id:”);
employeID = sc.nextInt();
System.out.println(“Enter DOJ (dd mm yyyy):”);
int dd = sc.nextInt(); int mm = sc.nextInt();
int yy = sc.nextInt(); dob = new Date(yy, mm, dd);
System.out.println(“Enter Salary:”);
salary = sc.nextInt();
}
void display1()
{ System.out.println(“EmployeeID:”+employeID);
System.out.println(“Salary:”+salary);
System.out.println(“DOJ:”+doj);
By: Shamama Anwar } } 41
Inheritance: Example 1
class Test
{ public static void main(String args[])
{ Employee e1 = new Employee();
el.getdata();
e1.getdata1();
e1.display();
e1.display1();
}
}
42
By: Shamama Anwar
Inheritance: Method Overriding
• When a method in a subclass has the same name and type signature as a
method in its super class, then the method in the subclass is said to override
the method in superclass.
44
By: Shamama Anwar
Inheritance: super keyword
• Calling the methods of the superclass
class A
{ void show()
{ System.out.println(“In Superclass:”); }
}
class B extends A
{ void show()
{ super.show();
System.out.println(“In Subclass:”); }
}
class test
{ public static void main(String args[])
{ B b = new B();
b.f1();
}
}
45
By: Shamama Anwar
Inheritance: Example 2
class Person
{ String name;
Date dob;
void getdata() { Scanner sc = new Scanner(System.in);
System.out.println(“Enter name:”);
name = sc.next();
System.out.println(“Enter DOB (dd mm yyyy):”);
int dd = sc.nextInt();
int mm = sc.nextInt();
int yy = sc.nextInt();
dob = new Date(yy, mm, dd); }
void display ()
{ System.out.println(“Name:”+name);
System.out.println(“DOB:”+dob);
}
}
46
By: Shamama Anwar
Inheritance: Example 2
class Employee extends Person
{ int employeID;
int salary;
Date doj;
void getdata() { super.getdata();
Scanner sc = new Scanner(System.in);
System.out.println(“Enter employee id:”);
employeID = sc.nextInt();
System.out.println(“Enter DOJ (dd mm yyyy):”);
int dd = sc.nextInt(); int mm = sc.nextInt();
int yy = sc.nextInt(); dob = new Date(yy, mm, dd);
System.out.println(“Enter Salary:”);
salary = sc.nextInt(); }
void display () { super.display();
System.out.println(“EmployeeID:”+employeID);
System.out.println(“Salary:”+salary);
System.out.println(“DOJ:”+doj);
By: Shamama Anwar } } 47
Inheritance: Example 2
class Test
{ public static void main(String args[])
{ Employee e1 = new Employee();
el.getdata();
e1.display();
}
}
48
By: Shamama Anwar
Inheritance: super keyword
• For accessing the member variables of the superclass
class A
{ int b = 30;
}
class B extends A
{ int b = 12;
void show()
{ System.out.println(“Subclass variable:”+b);
System.out.println(“Superclass variable:”+super.b);
}
}
class test
{ public static void main(String args[])
{ B b = new B();
b.show();
}
49
By:
} Shamama Anwar
Inheritance: super keyword
• For invoking the constructors of the superclass
class A
{ A() { System.out.println(“Constructor A”); } }
class B extends A
{ B() { System.out.println(“Constructor B”); } }
class C extends B
{ C() { System.out.println(“Constructor C”); } }
class test
{ public static void main(String args[])
{ C c = new C();
}
}
50
By: Shamama Anwar
Inheritance: super keyword
• For invoking the constructors of the superclass
class A
{ A() { System.out.println(“Constructor A”); } }
class B extends A
{ B() { System.out.println(“Constructor B”); } }
class C extends B
{ C() { System.out.println(“Constructor C”); } }
class test
{ public static void main(String args[])
{ C c = new C();
}
}
51
By: Shamama Anwar
Inheritance: super keyword
• For invoking the constructors of the superclass
class A
{ A() { System.out.println(“Constructor A”); } }
class B extends A
{ B() { System.out.println(“Constructor B”); }
B(int a){ a++; System.out.println(“Constructor B”+a);}}
class C extends B
{ C() { super(1);
System.out.println(“Constructor C”); } }
class test
{ public static void main(String args[])
{ C c = new C();
}
}
It is mandatory for a super statement in a constructor to be the first statement
within the constructor.
52
By: Shamama Anwar
Inheritance: Example 3
class Person
{ String name;
String dob;
Person(){ name = “”; dob = “”; }
Person(String x, String y)
{ name = x; dob = y; }
void display() {……… }
}
class Employee extends Person
{ int employeID;
int salary;
String doj;
Employee() { employeID = 0; salary = 0; doj = “”; }
Employee(String n, String b, int id, int sal, String j)
{ Person(n,b); employeID = id; salary = sal; doj = j;}
void display() {…….}
}By: Shamama Anwar 53
Inheritance: Example 3
class Test
{ public static void main(String args[])
{
//Get the input values from the user
Employee e1 = new Employee(“Abc”, “12/03/1980”, 1034,
70000, “07/12/2010”);
e1.display();
}
}
54
By: Shamama Anwar
final keyword
• To declare a constant (used with variable and argument declaration)
• To disallow method overriding (used with method declaration)
• To disallow inheritance (used with class declaration)
class Test
{ final void show()
{ System.out.println(“Superclass show”); }
}
class Test1 extends Test
{ void show() { System.out.println(“Subclass show”); }
}
class demo
{ Test1 obj = new Test1();
obj.show();
}
55
By: Shamama Anwar
final keyword
• To disallow inheritance (used with class declaration)
final class Test
{void show()
{ System.out.println(“Superclass show”); }
}
class Test1 extends Test
{ void show() { System.out.println(“Subclass show”); }
}
class demo
{ Test1 obj = new Test1();
obj.show();
}
56
By: Shamama Anwar
abstract keyword
• The abstract keyword can be used for defining methods and classes.
• A method that has been declared but not defined is called an abstract method.
public abstract void show();
57
By: Shamama Anwar
abstract keyword
• A class can be made abstract even if it does not contain any abstract
methods. This prevents the class from being instantiated.
• Abstract classes are good for defining a general category (template) containing
specific functions.
58
By: Shamama Anwar
abstract keyword: Example 1
abstract class Shape{ abstract void draw(); }
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle extends Shape{
void draw(){System.out.println("drawing circle");}
}
class TestAbstraction1{
public static void main(String args[]){
Circle s=new Circle();
s.draw();
}
}
59
By: Shamama Anwar
abstract keyword: Example 2
public abstract class Employee
{ private String name, address;
private int number;
public Employee( ) {. . . . }
public Employee(String n, String add, int num)
{ . . . }
public double computePay()
{ System.out.println("Inside Employee computePay");
return 0.0; }
public void mailCheck()
{ System.out.println("Mailing to " + this.name +
" " + this.address); }
public String getName() { return name; }
public String getAddress() { return address; }
public void setAddress(String newAddress)
{ address = newAddress; }
public int getNumber() { return number; } }
60
By: Shamama Anwar
abstract keyword: Example 2
public class AbstractDemo
{ public static void main(String [] args)
{ Employee e = new Employee(“Abc”, “Ranchi”, “2314”);
System.out.println("\n Call mailCheck using Employee
reference--");
e.mailCheck(); } }
61
By: Shamama Anwar
abstract keyword: Example 3
public abstract class Employee
{ private String name, address;
private int number;
public Employee( ) {. . . . }
public Employee(String n, String add, int num)
{ . . . }
public double computePay()
{ System.out.println("Inside Employee computePay");
return 0.0; }
public void mailCheck()
{ System.out.println("Mailing to " + this.name +
" " + this.address); }
public String getName() { return name; }
public String getAddress() { return address; }
public void setAddress(String newAddress)
{ address = newAddress; }
public int getNumber() { return number; } }
62
By: Shamama Anwar
abstract keyword: Example 3
public class Salary extends Employee
{ private double sal;
Salary(String n, String add, int num, double sal)
{ super(n, add, num);
setSalary(salary); }
public void mailCheck()
{System.out.println("Within mailCheck of Salary class ");
System.out.println("Mailing to " + getName() + " with
salary " + sal); }
public double getSalary() { return sal; }
public void setSalary(double newSal)
{ if(newSal >= 0.0) { sal = newSal; } }
63
By: Shamama Anwar
abstract keyword: Example 3
public class AbstractDemo
{ public static void main(String [] args)
{ Salary s = new Salary(“Abc", "UP", 1243, 3600.00);
System.out.println("Call mailCheck using Salary
reference --");
s.mailCheck();
} }
64
By: Shamama Anwar
Program
Create an abstract class Accounts with the following details:
• Data members: balance, accountNumber accountName,
address
• Methods: withdrawl( ) – abstract, deposit( ) – abstract
display( ) – to show the balance of the account
Create a subclass of this class SavingAccount and add the following details:
• Data members: rateOfInterest
• Methods: calulateAmount(),
display() – to display rate of interest with new balance and full
account holder details
Create another subclass of the Accounts class, CurrentAccount with the
following:
• Data Members: overdraftlimit
• Methods: display() – to show overdraft limit along with the full account
holder details.
Create objects of both the classes and use appropriate constructors.
65
By: Shamama Anwar
Interfaces
• In Java, only single inheritance is permitted. However, Java provides a construct
called as an interface which can be implemented by a class.
• A class can implement any number of interfaces. In effect using interfaces gives
us the benefit of multiple inheritance.
• Interfaces can contain only public, final and static variables. (by
default)
66
By: Shamama Anwar
Interfaces
• An interface is similar to an abstract class with the following exceptions:
• All methods defined in an interface are abstract. Interfaces can contain no
implementation.
• Interfaces cannot contain instance variables. However, they can contain
public static final variables (ie. constant class variables)
67
By: Shamama Anwar
Interface – Example 1
interface calculator
{
int add(int a, int b);
int sub(int a, int b);
int mult(int a, int b);
int div(int a, int b);
}
class demo implements calculator
{
public int add(int a, int b){ return a+b; }
public int sub(int a, int b){ return a-b; }
public int mult(int a, int b){ return a+b; }
public int div(int a, int b){ return a/b; }
}
68
By: Shamama Anwar
Interface – Example 2
interface area
{ double PI = 3.14;
int sq(int a);
int rect(int a, int b);
double cir(double r);
}
class demo implements area
{
public int sq(int a){ return a*a; }
public int rect(int a, int b){ return a*b; }
public double cir(double r){ return area.PI*r*r; }
}
69
By: Shamama Anwar
Extending Interfaces
• Just like normal classes, interfaces can also be extended.
• An interface can inherit another interface using the keyword extends (not
implements)
interface A { void showA(); }
interface B { . . . . . }
interface C extends B {. . . . }
interface D extends A, B { . . . . }
class A { . . . . }
interface B extends A { . . . . }
interface A { . . . . }
class B extends A { . . . . }
71
By: Shamama Anwar
Interfaces
interface A { . . . . }
class B implements A { . . . . . }
interface A { . . . . }
interface B { . . . . }
class C implements A, B { . . . . .}
interface A { . . . . }
class B { . . . . .}
72
By: Shamama Anwar
Interfaces
interface A { . . . . }
interface B { . . . . . }
class C { . . . . }
73
By: Shamama Anwar
Interfaces ‐ Example
1. Design an interface Queue with the following methods:
• Add elements
• Delete elements
• Check if queue is empty
2. Design an interface with a method reversal. This method takes a string as its
input and returns the reversed string.
74
By: Shamama Anwar
Interface vs. Abstract Class
75
By: Shamama Anwar
Packages
• Packages are a way to organize files into different directories according to
their functionality, usability as well as category.
• Packaging also help us to avoid class name collision when we use the same
class name as that of others.
package My;
class A
{ . . . . }
76
By: Shamama Anwar
Packages
package My;
public class example
{ public int fact(int a)
{ if(a==1)
return 1;
else
return a*fact(a-1); } }
package My1;
import My.*;
class demo
{ public static void main(String args[])
{ example e = new example();
System.out.println(“Factorial =”+e.fact(6));
}
}
77
By: Shamama Anwar
Access Modifiers
Private Default Protected Public
Same class
78
By: Shamama Anwar
X A
void abc()
private void def()
protected void xyz()
public void pqr()
B C
abc(), xyz(), pqr() abc(), xyz(), pqr()
Y
D E
xyz(), pqr() pqr()
79
By: Shamama Anwar