JavaProgramming_Lecture04
JavaProgramming_Lecture04
00
Object Oriented
Programming
Lecture - 4
Sept, 30th 2022 (4pm-7pm)
Learning outcomes:
• Inheritance
• implementing super-classes and a subclasses
• types of Inheritance
1. single class
2. multiple class (interface)
3. hierarchical class
4. hybrid class
-Reusing of the code
• Polymorphism
• Abstract Classes
• Interface Class
• Nested (or Inner) Classes – Classes of classes
Inheritance
• Inheritance is an important pillar of OOP(Object Oriented
Programming).
• It is the mechanism in Java by which one class is allowed to inherit the
features (fields and methods) of another class.
• Classes inherit from a super-class by the extend keyword
• Only 1 super-class can be extended at a time.
Inheritance Example:
• Child – Parents
• Car – Vehicle
• Dog – Animal
• Lecturer – Employee
• Hotel - Building
Inheritance Hierarchies
• Inheritance: the relationship between a more general class
(superclass) and a more specialized class (subclass).
• The subclass inherits data and behavior from the superclass.
Cars share the common traits of all vehicles
Example: the ability to transport people from one place to another
Master’s PhD
…
class SimpleGui extends JFrame {
…
}
Types of inheritance:
Hierarchical Inheritance.
An Example code Template:
// Consider the following
class cls1:
Source file Main_In.java Class file ClsA.java
class clsA
{ class Main_In class ClsA {
void add(int p,int q) { {
public static void int p;
System.out.println(p+q); main(String[] args) int q;
} { void add() {
}
// There is another // Create object of a subclass System.out.println("add = " +
ClsB o1 = new ClsB(); p+q); }
class cls2 which extends class // access field of superclass using the }
cls1: integer variables that stores the values //Class B inherit from class A
class clsB extends clsA o1.p = 5;
class ClsB extends ClsA {
o1.q = 6;
{ // call method of superclass using
void mul(int p,int q) void mul() {
object of subclass
System.out.println("product = " +
{ o1.add();
p*q); }
System.out.println(p*q); o1.mul();
}
} }
}
}
Output:
Run Main_In.java and ClsA.java files from your shell:
• Compile
• Output
Polymorphism defined
Inheritance Class File Polymorphism
Output:
Run Polymorphism.java (main class file) from your shell:
• Compile
• Output
}
} Output:
// Subclass (inherit from Animal)
class Cat extends Animal { Arhums-Air:Abstract_code arhumsultana$ javac Main.java
public void animalSound() { Arhums-Air:Abstract_code arhumsultana$ java Main
// The body of animalSound() is provided here The Cat says: MEWW MEAOO
System.out.println("The Cat says: MEWW
Zzz
MEAOO");
}
}
class Main {
public static void main(String[] args) {
Cat out = new Cat(); // Create a cat object
out.animalSound();
out.sleep();
}
}
Java Interface Class
covered in more detail next week
Inheritance
Java Nested Classes (Inner Classes)
class Outer {
int x = 10;
• Java Nested Classes are classes defined
inside other classes. // sub-classes are not automatically
created
• This is a naming convention only. Nested n = new Nested();
• think of it like a sub-directory
class Nested {
• dot notation is used for the name int y = 5; }
• e.g. System.out … }
• byte code shows how this works
public class Main {
• Nested class is not constructed public static void main(String[] args)
{
automatically when the outer class is Outer myNest = new Outer();
created. System.out.println(myNest.x + myNest.n.y);