2. Abstract Class
abstract class Shape
class Circle
extends
class Triangle
class Rectangle extends Shape Shape
extends
Shape
Java Programming: OOP 2
3. What is an Abstract Class?
âą A class that is declared abstract
» Ex - abstract class Demo
» It may or may not use abstract
methods
Java Programming: OOP 3
4. What is an Abstract Method ?
âą A method that is declared abstract
âą A method that is declared without an
implementation
â Ex - abstract void add(int x, int y);
Java Programming: OOP 4
5. Example
public abstract class Shape {
//Abstract method i.e no implementation
abstract void Area();
}
public class Rectangle extends Shape { public class Circle extends Shape {
@Override @Override
void Area() { void Area() {
Double area = length * width; Double area = 3.14 * radius*radius;
}
} }
} Java Programming: OOP 5
7. Interface
âą Interfaces are declared using
the interface keyword
âą Interface can contain :
» method signature
(no implementation)
» constant declarations
(variable declarations that are declared to be both static and final)
Java Programming: OOP 7
8. Interface
âą A class that implements an interface must implement
all of the methods described in the interface
implements
Interface Class
abstract Method1(); abstract Method1();
abstract Method2(); abstract Method2();
abstract Method3(); abstract Method3();
Java Programming: OOP 8
9. How to create an Interface
public interface Demo_interface {
int add(int value1, int value2); Demo_interface
void print(int sum);
int add(int value1, intvalue2);
void print(int sum);
}
Java Programming: OOP 9
10. How to implement the Interface
public class Class1 implements Demo_interface { Demo_interface
@Override
public int add(int value1, int value2) {
int add(int value1, intvalue2);
int sum = value1 + value2;
return sum; void print(int sum);
}
@Override
public void print(int sum) {
Class1
System.out.println("The sum is : " + sum);
}
public int add(int value1, int value2);
}
Java Programming: OOP public void print(int sum); 10
11. Using it in the main class
public class Execute {
public static void main(String[] args) {
Execute
Class1 ob = new Class1();
ob
int sum = ob.add(10, 10);
sum = ob.add(10,10);
ob.print(sum);
ob.print(sum);
}
}
Java Programming: OOP 11
13. Enumeration
âą Enumerations helps to relate Select one
the variables with related Sunday
constants so that it will be Monday
Tuesday
flexible to work. Wednesday
âą To represent we use âenumâ Thursday
keyword. Friday
âą It can be used in dropdown Saturday
boxes.
14. Why we need to use Enum?
âą Enum is type-safe i.e any constants can not be
assigned to that variables outside the enum
definition.
âą Adding new constants will be easy without
disturbing already present code.
âą You can also assign different constants to variables
other than default values.
15. How to declare an Enum?
âą Declaring an enum is similar to class.
âą Should be declared outside the class in which it has to be used or in an
interface.
variables which will be assigned constant values
âą enum Colors{
Red, Green, Blue, White, Yellow
}
name of enum 0 1 2 3 4(default constants
assigned)
16. Simple program for Enum
enum Colors_enum{red , green , blue , white , yellow}
public class Main { Output:
public static void main(String args[]) {
red
Colors_enum colors[]=Colors_enum.values(); green
for(Colors_enum c:colors) blue
{ white
System.out.println(c); yellow
}
}
}
17. How to assign constants to Enum by user
enum Chocolates{ public class Main {
dairymilk(20) , public static void main(String args[]) {
kitkat(10) , Chocolates favouritechoco=Chocolate.dairymilk;
munch(5); switch(favouritechoco)
{
int cost; case dairymilk: System.out.println(âDairy Milkâ);
Choloclates(int cost) break;
{ case kitkat: System.out.println(âKitkatâ);
break;
this.cost=cost; case munch: System.out.println(âMunchâ);
} break;
}
Output: Diary Milk }
}