Define
abstract class:
Abstraction:
Abstraction is the concept of displaying only the essential features of an
object while hiding the details. It focuses on what an object does rather than
how it does it.
// abstraction : displaying only functionality but not in detail
·
To define an abstract class, use the abstract keyword.
·
Abstract k/w can be used to define abstract class and abstract methods
// define abstract class : we have to use abstract k/w
// can be used to define abstract class and
abstract methods
// abstract class : if u define any class with
abstract k/w -
Syntax :
abstract
class someClassName // abstr class
{
}
ex1:
package AbstractClassBasicss;
public abstract class A // abstract class
{
// abstract Methods and non abstr methods
(concrete method- method with body)
//
//
Define abstract method- M1
// is incomplete method - dont define method
body
// abstract void M1()
// {
//
// }
abstract
void M1();// abstr method -incomplete -
not a complete method
// don’t specify method body for abstract
methods
//
Define abstr method M2
//
abstr method
abstract
void M2();
// Define non abstr method -M3()
// non abstr methods (or) concrete method (or)
complete methods or Method with body
void
M3()
{
System.out.println("M3
");
}
// Define non abstr method -M4()
//
non abstr method
void
M4()
{
System.out.println("M4");
}
}
-------------------
package AbstractClassBasicss;
public class B extends A
//
{ // abstract methods will be implemented in
child class
// The type B must implement the
inherited abstract method A.M1()
//
Note : we must implement / write method
body// define method body in child class. else it throws CE
// implement abstr method M1 from class -A
void
M1()
{
System.out.println("M1");
}
// implement abstr method M2 from
class -A -- at least write define body
without any code
void
M2()
{
System.out.println("M2");
}
public
static void main(String[] args) {
//
FAQ : Can we create obj for abstact
class- A ?
// A aref = new A();
//
CE: Cannot instantiate the type A
//
cannot create obj for abstract class -A
// Create obj for class - B
B
bref = new B();
// call method M1(), M2()
bref.M1();
bref.M2();
bref.M3();
}
}
o/p:
M1
M2
M3
ex2:
abstract class car // abstract class
{
// contains abstract Methods + non
abstract methods
//
Define abstract methods: startCar()
// abs
method means - is incomplete method -
//
We dont define method body
// {
//
// }
//
if we dont define method body, some where we should define method body
// usually in child classes , we will
define method body for all abstract methods
//Define
abstrr method -speedofCar()
// CE : Abstract methods do not specify a body
// { //
if we define method body for abstarct method-- it throws error
//
// }
//
Define stopcar()
// non
abstr methods: is complete method -- we
define method body
// or Concrete method
//
Define method body
System.out.println("stop car - non abs method");
}
// to define / implement abstract methods
--- we need one more class
class MaruthiCar extends car //
non abstract class
{
//
The type MaruthiCar must implement the inherited abstract method
car.speedofCar()
// add all unimplemented Methods
//
if we dont define method body for abst methods in child class, it throws error-
The type MaruthiCar must implement the inherited abstract method
car.speedofCar()
//
implement startCar()
System.out.println("
to start car- insert keys and turn to the right side");
//
implement speedofCar()
//
define method body for abstr method i.e speedofCar
System.out.println("speedofCar-
must be 1-150 k/ hr ");
//
CE : The type MaruthiCar must implement the inherited abstract method
car.startCar()
//
Define method body for abst methods
}
public class TestCar // non abstract class
{
// abstract class : if u define any class with
abstract k/w -
public
static void main(String[] args)
{
// 1 . We cannot create any obj - for abstract
classes
//
Error : Cannot
instantiate the type car
// we cannot create obj for car - class as car
class is abstr class
//
FAQ Can we create obj for abstr class?
//create
obj for maruthicar class
//
call startCar()
//
call speedofCar();
//
call stopcar();
}
}
o/p:
to start car- insert keys and
turn to the right side
speedofCar- must be 1-150 k/ hr
stop car - non abs method
HW : create some abstract class for printer, child classes-( HPPrinter,
DellPrinter) and Define method body for
abst methods?
FAQ Can we have constructor in
abstract class ?
package AbstractClassBasicss;
abstract class car // abstract class
{
int
a =10 ;
//
FAQ Can we have constr in abstract class ??
//
Define constr
car()
{
System.out.println("Dc
from abstr class");
}
//
Define abstr M1()
abstract
void M1(); // is incomplete
method
// dont define method body
}
package AbstractClassBasicss;
public class TestCar extends car
// non abstract class
{
// get
parent class var + parent class methods into child class
// a
and M1() int a =10 ;
// //
FAQ Can we have constr in abstract class ??
// //
Define constr
// car()
// {
// System.out.println("Dc
from abstr class");
// }
// //
Define abstr M1()
// abstract
void M1(); // is incomplete
method
// // dont define method body
//
//
define abstr method with body
void
M1()
{
System.out.println("Defining
method body in child class");
}
//
define constr -TestCar
TestCar()
{
// Before executing child class constr// first
it executes parent class const, then child class constr
System.out.println("child
class constr");
}
public
static void main(String[] args)
{
// to call constr-- we must create obj
// without creating obj- we cannot call any constr
//create
obj for abstr class- car
// car cref = new car();// CE // we cannot create obj for abst class
//create
obj for class- TestCar
TestCar
tcar = new TestCar();
// display var -a
System.out.println("tcar
a ="+ tcar.a);
//
call M1()
tcar.M1();
}
}
o/p:
Dc from abstr class
child class constr
tcar a =10
Defining method body in child
class
No comments:
Post a Comment