SlideShare ist ein Scribd-Unternehmen logo
1 von 19
Abstract Class
Abstract Class

                       abstract class Shape




                                   class Circle
                                     extends
                                                        class Triangle
class Rectangle extends Shape         Shape
                                                           extends
                                                            Shape

                                Java Programming: OOP                    2
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
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
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
Interface
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
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
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
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
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
Enums in Java
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.
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 {                                     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
           }
    }
 }
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      }
                          }
‱Q& A..?
Thanks..!

Weitere Àhnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
Java Unit 2(part 3)
Java Unit 2(part 3)Java Unit 2(part 3)
Java Unit 2(part 3)
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Java Unit 2 (Part 2)
Java Unit 2 (Part 2)Java Unit 2 (Part 2)
Java Unit 2 (Part 2)
 
Method overloading, recursion, passing and returning objects from method, new...
Method overloading, recursion, passing and returning objects from method, new...Method overloading, recursion, passing and returning objects from method, new...
Method overloading, recursion, passing and returning objects from method, new...
 
Taming Java Agents
Taming Java AgentsTaming Java Agents
Taming Java Agents
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java Reflection
Java ReflectionJava Reflection
Java Reflection
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
Pythonpresent
PythonpresentPythonpresent
Pythonpresent
 
Java basic
Java basicJava basic
Java basic
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
 
Unit i
Unit iUnit i
Unit i
 
oops-1
oops-1oops-1
oops-1
 

Andere mochten auch (7)

Java class 8
Java class 8Java class 8
Java class 8
 
Java class 7
Java class 7Java class 7
Java class 7
 
Java class 1
Java class 1Java class 1
Java class 1
 
Java
Java Java
Java
 
Java class 5
Java class 5Java class 5
Java class 5
 
What Is Salesforce CRM? | Salesforce CRM Tutorial For Beginners | Salesforce ...
What Is Salesforce CRM? | Salesforce CRM Tutorial For Beginners | Salesforce ...What Is Salesforce CRM? | Salesforce CRM Tutorial For Beginners | Salesforce ...
What Is Salesforce CRM? | Salesforce CRM Tutorial For Beginners | Salesforce ...
 
Chapter 9 Abstract Class
Chapter 9 Abstract ClassChapter 9 Abstract Class
Chapter 9 Abstract Class
 

Ähnlich wie Java class 4

Implementation of interface9 cm604.30
Implementation of interface9 cm604.30Implementation of interface9 cm604.30
Implementation of interface9 cm604.30
myrajendra
 
C# for Java Developers
C# for Java DevelopersC# for Java Developers
C# for Java Developers
Jussi Pohjolainen
 
Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31
myrajendra
 
14.jun.2012
14.jun.201214.jun.2012
14.jun.2012
Tech_MX
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
HCMUTE
 
CodeFest 2010. Đ˜ĐœĐŸĐ·Đ”ĐŒŃ†Đ”ĐČ Đ˜. — Fantom. Cross-VM Language
CodeFest 2010. Đ˜ĐœĐŸĐ·Đ”ĐŒŃ†Đ”ĐČ Đ˜. — Fantom. Cross-VM LanguageCodeFest 2010. Đ˜ĐœĐŸĐ·Đ”ĐŒŃ†Đ”ĐČ Đ˜. — Fantom. Cross-VM Language
CodeFest 2010. Đ˜ĐœĐŸĐ·Đ”ĐŒŃ†Đ”ĐČ Đ˜. — Fantom. Cross-VM Language
CodeFest
 

Ähnlich wie Java class 4 (20)

Implementation of interface9 cm604.30
Implementation of interface9 cm604.30Implementation of interface9 cm604.30
Implementation of interface9 cm604.30
 
Interface
InterfaceInterface
Interface
 
Interface
InterfaceInterface
Interface
 
Java introduction
Java introductionJava introduction
Java introduction
 
C# for Java Developers
C# for Java DevelopersC# for Java Developers
C# for Java Developers
 
Interface in java
Interface in javaInterface in java
Interface in java
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
 
Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31
 
14.jun.2012
14.jun.201214.jun.2012
14.jun.2012
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 
Cse java
Cse javaCse java
Cse java
 
Abstraction
AbstractionAbstraction
Abstraction
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.ppt
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
2. overview of c#
2. overview of c#2. overview of c#
2. overview of c#
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
CodeFest 2010. Đ˜ĐœĐŸĐ·Đ”ĐŒŃ†Đ”ĐČ Đ˜. — Fantom. Cross-VM Language
CodeFest 2010. Đ˜ĐœĐŸĐ·Đ”ĐŒŃ†Đ”ĐČ Đ˜. — Fantom. Cross-VM LanguageCodeFest 2010. Đ˜ĐœĐŸĐ·Đ”ĐŒŃ†Đ”ĐČ Đ˜. — Fantom. Cross-VM Language
CodeFest 2010. Đ˜ĐœĐŸĐ·Đ”ĐŒŃ†Đ”ĐČ Đ˜. — Fantom. Cross-VM Language
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...
 

Mehr von Edureka!

Mehr von Edureka! (20)

What to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | EdurekaWhat to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | Edureka
 
Top 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | EdurekaTop 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | Edureka
 
Top 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | EdurekaTop 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | Edureka
 
Tableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | EdurekaTableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | Edureka
 
Python Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaPython Programming Tutorial | Edureka
Python Programming Tutorial | Edureka
 
Top 5 PMP Certifications | Edureka
Top 5 PMP Certifications | EdurekaTop 5 PMP Certifications | Edureka
Top 5 PMP Certifications | Edureka
 
Top Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | EdurekaTop Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | Edureka
 
Linux Mint Tutorial | Edureka
Linux Mint Tutorial | EdurekaLinux Mint Tutorial | Edureka
Linux Mint Tutorial | Edureka
 
How to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| EdurekaHow to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| Edureka
 
Importance of Digital Marketing | Edureka
Importance of Digital Marketing | EdurekaImportance of Digital Marketing | Edureka
Importance of Digital Marketing | Edureka
 
RPA in 2020 | Edureka
RPA in 2020 | EdurekaRPA in 2020 | Edureka
RPA in 2020 | Edureka
 
Email Notifications in Jenkins | Edureka
Email Notifications in Jenkins | EdurekaEmail Notifications in Jenkins | Edureka
Email Notifications in Jenkins | Edureka
 
EA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | EdurekaEA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | Edureka
 
Cognitive AI Tutorial | Edureka
Cognitive AI Tutorial | EdurekaCognitive AI Tutorial | Edureka
Cognitive AI Tutorial | Edureka
 
AWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | EdurekaAWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | Edureka
 
Blue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | EdurekaBlue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | Edureka
 
Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka
 
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | EdurekaA star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
 
Kubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | EdurekaKubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | Edureka
 
Introduction to DevOps | Edureka
Introduction to DevOps | EdurekaIntroduction to DevOps | Edureka
Introduction to DevOps | Edureka
 

KĂŒrzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

KĂŒrzlich hochgeladen (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

Java class 4

  • 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 } }