Abstract Class
Abstract Class                       abstract class Shape                                   class Circle                  ...
What is an Abstract Class?• A class that is declared abstract             » Ex - abstract class Demo             » It may ...
What is an Abstract Method ?• A method that is declared abstract• A method that is declared without an  implementation   –...
Example                            public abstract class Shape {                            //Abstract method i.e no imple...
Interface
Interface• Interfaces are declared using  the interface keyword• Interface can contain :            » method signature    ...
Interface• A class that implements an interface must implement  all of the methods described in the interface             ...
How to create an Interfacepublic interface Demo_interface {int add(int value1, int value2);               Demo_interfacevo...
How to implement the Interfacepublic class Class1 implements Demo_interface {          Demo_interface@Overridepublic int a...
Using it in the main classpublic class Execute {public static void main(String[] args) {                                  ...
Enums in Java
Enumeration • Enumerations helps to relate   Select one   the variables with related       Sunday   constants so that it w...
Why we need to use Enum?• Enum is type-safe i.e any constants can not be  assigned to that variables outside the enum  def...
How to declare an Enum? •   Declaring an enum is similar to class. •   Should be declared outside the class in which it ha...
Simple program for Enum enum Colors_enum{red , green , blue , white , yellow} public class Main {                         ...
How to assign constants to Enum by userenum Chocolates{          public class Main {dairymilk(20) ,           public stati...
•Q& A..?
Thanks..!
Upcoming SlideShare
Loading in …5
×

Java class 4

1,894 views
1,657 views

Published on

Published in: Technology, News & Politics
1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total views
1,894
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
119
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

Java class 4

  1. 1. Abstract Class
  2. 2. Abstract Class abstract class Shape class Circle extends class Triangleclass Rectangle extends Shape Shape extends Shape Java Programming: OOP 2
  3. 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. 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. 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 @Overridevoid Area() { void Area() {Double area = length * width; Double area = 3.14 * radius*radius; }} }} Java Programming: OOP 5
  6. 6. Interface
  7. 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. 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. 9. How to create an Interfacepublic interface Demo_interface {int add(int value1, int value2); Demo_interfacevoid print(int sum); int add(int value1, intvalue2); void print(int sum);} Java Programming: OOP 9
  10. 10. How to implement the Interfacepublic class Class1 implements Demo_interface { Demo_interface@Overridepublic int add(int value1, int value2) { int add(int value1, intvalue2);int sum = value1 + value2;return sum; void print(int sum);}@Overridepublic void print(int sum) { Class1System.out.println("The sum is : " + sum);} public int add(int value1, int value2);} Java Programming: OOP public void print(int sum); 10
  11. 11. Using it in the main classpublic class Execute {public static void main(String[] args) { ExecuteClass1 ob = new Class1(); obint sum = ob.add(10, 10); sum = ob.add(10,10); ob.print(sum);ob.print(sum);}} Java Programming: OOP 11
  12. 12. Enums in Java
  13. 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. 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. 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. 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. 17. How to assign constants to Enum by userenum 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 } }
  18. 18. •Q& A..?
  19. 19. Thanks..!

×