java class 4
Post on 22-Apr-2015
1.348 Views
Preview:
DESCRIPTION
TRANSCRIPT
Abstract Class
Java Programming: OOP 2
Abstract Class
abstract class Shape
class Rectangle extends Shape
class Circle extends Shape class Triangle
extends Shape
Java Programming: OOP 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 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 5
Example
public class Circle extends Shape {
@Overridevoid Area() {Double area = 3.14 * radius*radius;}}
public abstract class Shape {
//Abstract method i.e no implementationabstract void Area();}public class Rectangle extends Shape {
@Overridevoid Area() {Double area = length * width;
}}
Interface
Java Programming: OOP 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 8
Interface
• A class that implements an interface must implement all of the methods described in the interface
Interface Classimplements
abstract Method1();
abstract Method2();
abstract Method3();
abstract Method1();
abstract Method2();
abstract Method3();
Java Programming: OOP 9
How to create an Interface
public interface Demo_interface {
int add(int value1, int value2);void print(int sum);
}
Demo_interface
int add(int value1, intvalue2);
void print(int sum);
Java Programming: OOP 10
How to implement the Interface
public class Class1 implements Demo_interface {@Overridepublic int add(int value1, int value2) {int sum = value1 + value2;return sum;}@Overridepublic void print(int sum) {System.out.println("The sum is : " + sum);}}
Class1
public int add(int value1, int value2);
public void print(int sum);
Demo_interface
int add(int value1, intvalue2);
void print(int sum);
Java Programming: OOP 11
Using it in the main class
public class Execute {
public static void main(String[] args) {
Class1 ob = new Class1();
int sum = ob.add(10, 10);
ob.print(sum);}}
Execute
ob
sum = ob.add(10,10);
ob.print(sum);
Enums in Java
Enumeration
• Enumerations helps to relate the variables with related constants so that it will be flexible to work.
• To represent we use “enum” keyword.
• It can be used in dropdown boxes.
Select oneSundayMondayTuesday
WednesdayThursday
Friday
Saturday
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.
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)
Simple program for Enum
enum Colors_enum{red , green , blue , white , yellow}public class Main {public static void main(String args[]) {
Colors_enum colors[]=Colors_enum.values();for(Colors_enum c:colors)
{System.out.println(c);
} }}
Output:
redgreenbluewhiteyellow
How to assign constants to Enum by user
enum Chocolates{dairymilk(20) ,
kitkat(10) , munch(5);
int cost;Choloclates(int cost){this.cost=cost;}
public class Main {public static void main(String args[]) {
Chocolates favouritechoco=Chocolate.dairymilk;switch(favouritechoco){ case dairymilk: System.out.println(“Dairy Milk”);
break; case kitkat: System.out.println(“Kitkat”);
break; case munch: System.out.println(“Munch”);
break;}
}}
Output: Diary Milk
•Q& A..?
Thanks..!
top related