Sunteți pe pagina 1din 38

Java Abstract Class

Abstract class

● Abstraction is a process of hiding the implementation details and showing only functionality to the
user.
● process of highlighting the set of services and hide the implementations
● using abstract class and interface achieves Abstraction
● it is used to hide implementation details of an object in an application
● abstract class:
● 1)abstract method (a method without body)
● 2)non abstract method (a method with body)
● we create subclasses for an abstract class ,the subclass implements abstract methods.
abstract methods:
● contains only method declaration but not its implementation and end with semicolon, use the
abstract keyword Ex:abstract void m1();
● abstract class Test1//Test1 is Abstract class
● {
● void m1(){}//normal method
● abstract void m2();//abstract method
● }
Abstract class

● for abstract class object creation is not possible


● abstract class
● {
● void m1(){}
● void m2(){}
● }//it is also abstract class ,but object is not created.
● abstract class may or may not contains abstract methods, but object
creation is not possible

● There are two ways to achieve abstraction in java


● Abstract class (0 to 100%)
● Interface (100%)
Abstract class
● package com.ethnus;
● abstract class Sup
● {
● abstract void m1();
● void m2()
● {
● System.out.println("iam m2 method");
● }
● }
● class Sub extends Sup
● {
● void m1()
● {
● System.out.println("m1 is abstact method is implemented bysub class");
Abstract class

● In reality IceCream can exist, even with out FruitSaladWithIceCream, where


as Entertainment can not exist on its own. It has to be either Movie, Drama or
Circus or some other entertainment.
● Similarly Metal is an abstract class, since for we to show the Metal, we need
to either show Gold or Iron or some thing else. If we had Mobile and
MobileWithCamera, then Mobile would be a concrete class since it can exists
on its own.
● IceCream and Mobile are examples of concrete super-classes, where as
Entertainment, Metal and Shape are examples of abstract super-classes.
Abstract class

In Java every class defined is by default concrete class, but if we want to


implement abstract class, we need to use the abstract keyword before the class
to mark a class as abstract class.
abstract

abstract class Shape


{
}

class Rectangle extends Shape


{
}
Abstract class

Here we have created an abstract class called Shape and a concrete class called
Rectangle. Abstract class is prefixed with the keyword abstract, where as for
concrete class we need not mention any keyword.
Shape shape1; // Can create a reference of abstract class
Shape shape2 = new Shape(); // WILL NOT WORK. Can not create an object of abstract class

Rectangle rect1 = new Rectangle(); // Can create reference and object of a concrete class
shape1 = rect1; // Can assign concrete sub-class reference to abstract super-class reference
Rules For Abstract Methods and Abstract Classes

1) Any class that extends an abstract class must implement all abstract methods of
the super class, unless the subclass is also abstract.
2) A method can never, ever, ever be marked as both abstract and final, or both
abstract and private
3) Finally, you need to know that the abstract modifier can never be combined with
the static modifier
4) An abstract class can never be instantiated.
5) We can create a reference of abstract class
6) inside the abstract class constructor is possible
7) we can use instance and static blocks inside the abstract class
Rule1
● abstract class Test1{
● abstract void m1();
● abstract void m2();
● abstract void m3();}
● abstract class Test2 extends Test1
● {
● void m1(){
● System.out.println("m1 method");
● //here m2(),m3() implemenations are not here.i.e class is Abstract}}
● public class Test3 extends Test2 {
● void m2()
● {System.out.println("m2 method");}
● void m3()
● {
● System.out.println("m3 method");}
Rule4

● package com.ethnus;

● public abstract class Test3 {

● public static void main(String[] args) {


● //Test3 t=new Test3(); object creation is not possible
● System.out.println("hello");
}
}
Rule6

● abstract class Sample1


● {Sample1(){
● System.out.println("abstract class constructor");
● }
● abstract void m1();
● }
● public class Sample extends Sample1{
void m1(){
● System.out.println("m1 method");
● }
● Sample()
● {super();
● System.out.println("normal class constructor");
● }
Rule5

● abstract class Sup


● {abstract void m1();
● void m2()
● {
● System.out.println("m3 method");
● }}
● public class Sub extends Sup {
● void m1() {
● System.out.println("m1 method");}
● public static void main(String[] args) {
● }
● Sup s1=new Sub();
● s1.m1();
● s1.m2();
Rule7

● abstract class Sample1


● {
● Sample1()
● {
● System.out.println("abstract class constructor");
● }
● {
● System.out.println("instance block");

● }
● static
● {
● System.out.println("static block");
● }
● public class Sample extends Sample1{

● Sample()
● {
● super();
● System.out.println("normal class constructor");
● }
● public static void main(String[] args) {
● new Sample();

● }

● }
Abstract class

aabstract class A
{
// Valid, even with out any abstract methods
}

class B // Invalid, class B should be abstract, since it has abstract method.


{
abstract void method1();
}
An abstract class can have one or more abstract methods.

aabstract class C
{
abstract void method1();

abstract double method2(int x, int y);

abstract boolean method3(char z);


}
An abstract class can have both abstract and non abstract (or concrete)
method.
aabstract class D
{
void method1()
{
System.out.println("I am a concrete method");
}

abstract double method2(int x, int y);

int method3(double z)
{
System.out.println("I am also a concrete method");
}

abstract boolean method4(char z);


}
The abstract method should not have method body. Even empty flower
braces { } are not allowed.
a
abstract class A
{
abstract void method1(); // Valid

abstract void method2() {} // Invalid - since it has method body

Any sub-class extending from an abstract class should either implement all the
abstract methods of the super-class or the sub-class itself should be marked as
abstract.
Abstract class

abstract class A{ class D extends A{


a abstract void method1(); void method1(){
abstract void method2(); System.out.println("Method1 implemented here.");
} }
class B extends A{ // Invalid, class D should be marked as abstract, since
// Invalid since B does not implement method2 is not implemented.
the abstract methods }
} abstract class E extends A{
abstract class C extends A{ void method1(){
// Valid since C is marked as abstract, System.out.println("Method1 implemented here.");
even though the abstract methods are }
not implemented, // Even though method2 is not implemented, class D is
} marked as abstract, so it is Valid.
}
Abstract class

aabstract class X{
abstract void method1();
abstract void method2();
}
abstract class Y extends X{
void method1(){
System.out.println("Method1 implemented here.");
}}
class Z extends Y{
void method2(){
System.out.println("Method2 implemented here.");
}
}
Abstract class

It is not necessary to add the abstract methods only in the super most class, we
can add more abstract methods in the sub-classes.
abstract class X class Z extends Y
{ {
abstract void method1(); void method1()
} {
System.out.println("Method1 from class X implemented
abstract class Y extends X here.");
{ }
abstract void method2();
} void method2()
{
System.out.println("Method1 from class Y implemented
here.");
}
}
Abstract class

class F extends A
{a
// Valid since both methods are implemented here.
void method1()
{
System.out.println("Method1 implemented here.");
}

void method2()
{
System.out.println("Method2 implemented here.");
}
}

If an abstract class contains multiple methods, it is not necessary that all the methods of
the abstract class are implemented in the immediate sub-class. Few of them can be
implemented in sub-sub-classes or any where else in the sub-class hierarchy. But for a
class to be concrete, all the abstract methods in its super-class must be implemented.
Abstract class

Question:
● Emp (cal_sal())
● Part_Emp (cal_sal())
● Full_Emp (cal_sal())
● where Emp is the abstract class ,having cal_sal() abstract methods.
● and 2 subclasses Part_Emp(),Full_Emp() ,these implements cal_sal() according
to the type of employee.
● for an abstract class we cannot create an object.
● package com.ethnus;
● import java.util.Scanner;
● abstract class Emp
● {
● int eid;
● String ename;
● String dept;
● public Emp(int eid, String ename, String dept) {
● this.eid = eid;
● this.ename = ename;
● this.dept = dept;
● System.out.println("NAME:"+ename);
● System.out.println("DEPT NAME:"+dept);
● System.out.println("Emp Id:"+eid);
● }
● abstract void cal_salary();
● class Full_Emp extends Emp
● {
● Full_Emp(int eid,String ename,String dept)
● {
● super(eid,ename,dept);
● }
● void cal_salary()
● {
● Scanner sc=new Scanner(System.in);
● System.out.println("enter the no of days");
● int nod=sc.nextInt();
● System.out.println("enter the amount per day");
● Double amt=sc.nextDouble();
● System.out.println("SALARY OF FULL TIME EMPLOYEE: "+amt*nod);
● }
● }
● class Part_Emp extends Emp
● {
● Part_Emp(int eid,String ename,String dept)
● {
● super(eid,ename,dept);
● }
● void cal_salary()
● {
● Scanner sc=new Scanner(System.in);
● System.out.println("enter the no of hours");
● int nod=sc.nextInt();
● System.out.println("enter the amount per hour");
● Double amt=sc.nextDouble();
● System.out.println("SALARY OF PART TIME EMPLOYEE: "+amt*nod);
● }
● }
● public class Test4 {

● public static void main(String[] args) {


● Full_Emp f=new Full_Emp(100,"venu","Elecricity");
● f.cal_salary();
● Part_Emp p=new Part_Emp(500,"chandhu","Irrigation");
● p.cal_salary();
● }

● }
Abstract class: Assignment1

● Take one abstract class (Super1)


● superclass( calculate(int) is the abstract method)
● Take 2 sub classes called Sub1 and Sub2
● subclass1 (calculate(int) square)
● subclass2 (calculate(int) sqrt)
● Implements the Abstraction with the 3 classes
● package com.ethnus;

● abstract class Super1


● {
● abstract void cal(int x);
● }
● class Sub1 extends Super1
● {
● void cal(int x)
● {
● System.out.println("the square:"+x*x);

● }
● }
● class Sub2 extends Super1
● {
● void cal(int x)
● {
● System.out.println("the sqrt of a no:"+Math.sqrt(x));

● }
● }
● class Demo {
● public static void main(String[] args)
● {
● Sub1 s1=new Sub1();
● s1.cal(10);
● Sub2 s2=new Sub2();
● s2.cal(10);

● }
Assignment:2
● Shape //abstract class
● color:String
● getArea():double//abstract methods

● Rectangle
● length:double
● breadth:double
● getArea():double //length*breadth

● Triangle
● radius:double
● getArea():double //using Math.PI *radius*radius
Calculate Areas: Assignment

For e.g., we know that every Shape has an area, but we do not know how to
calculate the area, until we know what Shape it is. This is because the area
calculation for Rectangle is different from the area calculation of Triangle, which
is different from that of the Circle. So we will declare that we have a method
called getArea() in Shape, but only define or implement the logic of calculating
the area in there respective concrete sub-classes.
DESCRIPTION

Here we have created an abstract Shape class and two concrete classes namely
Rectangle and Circle which extends from the Shape class. We have
implemented the getArea() method in the sub-classes. Please note that every
sub-class extending from an abstract class should implement all the abstract
methods, otherwise it will cause compilation errors.
THINGS TO TRY

Define one more sub-class Triangle which extends from Shape class. Create a
constructor, which takes the parameters base and height. Also implement the
getArea() method. Area of triangle is (base * height) / 2.0

Add one more abstract method getPerimeter() in the Shape class and
implement that method in Rectangle and Circle.

Define one more sub-class Polygon which extends from Shape. Do not define
any constructors or methods in that class including the getArea() method.
Observe the compilation error you get.
THANK YOU

S-ar putea să vă placă și