Documente Academic
Documente Profesional
Documente Cultură
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
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;
● 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
}
aabstract class C
{
abstract void method1();
int method3(double z)
{
System.out.println("I am also a concrete method");
}
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
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 {
● }
Abstract class: Assignment1
● }
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