19
Abstract Class

Java class 4

  • Upload
    edureka

  • View
    1.342

  • Download
    1

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Java class 4

Abstract Class

Page 2: Java class 4

Java Programming: OOP 2

Abstract Class

abstract class Shape

class Rectangle extends Shape

class Circle extends Shape class Triangle

extends Shape

Page 3: Java class 4

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

Page 4: Java class 4

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);

Page 5: Java class 4

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;

}}

Page 6: Java class 4

Interface

Page 7: Java class 4

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)

Page 8: Java class 4

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();

Page 9: Java class 4

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);

Page 10: Java class 4

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);

Page 11: Java class 4

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);

Page 12: Java class 4

Enums in Java

Page 13: Java class 4

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

Page 14: Java class 4

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.

Page 15: Java class 4

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)

Page 16: Java class 4

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

Page 17: Java class 4

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

Page 18: Java class 4

•Q& A..?

Page 19: Java class 4

Thanks..!