## ABSTRACT CLASS ##
* A class declared by using abstract keyword.
example-
abstract class Foo
{
}
* There are two differences between the abstract-class and non-abstract-class (also known as concrete class)
1. we cant create object of abstract class.
2. abstract class may have abstract methods.
note: except these two differences everything is same
* We can inherit abstract class but its object cant be created, however their reference variable can be created.
example-
* A class declared by using abstract keyword.
example-
abstract class Foo
{
}
* There are two differences between the abstract-class and non-abstract-class (also known as concrete class)
1. we cant create object of abstract class.
2. abstract class may have abstract methods.
note: except these two differences everything is same
* We can inherit abstract class but its object cant be created, however their reference variable can be created.
example-
Foo f = new Foo(); it is not valid
Foo f; it is valid
Foo f = null; it is also valid, null means no object
class Foo extends Doo // it is also valid, abstract class can be inherited
{
}
* static members (static variables and static methods) of abstract class can be accessed using the class-name
example-
abstract class Foo
{
static int a = 1;
static void b()
{
}
}
class Test
{
public static void main(String[] args)
{
// access static variable of abstract class
Foo.a = 1;
// call static method of abstract class
Foo.b();
}
}
* non static members (non static variables and non static methods) of abstract class can be accessed using following guidelines
step-1: inherit the abstract class
step-2: assign the object of subclass inside the reference-variable of super-class (the polymorphic assignment)
step-3: access the non static members on this reference-variable
example-
abstract class Foo
{
int a = 1;
void b()
{
}
}
// step-1 --> inherit abstract class
class Test extends Foo
{
public static void main(String[] args)
{
// step-2 --> assign the object of subclass inside the reference-variable
// of super-class (the polymorphic assignment)
Foo f = new Test();
// access non static variable of super-class
f.a = 1;
// call non static method of super-class
f.b();
}
}
# ABSTRACT METHOD #
* A method declared by using abstract keyword.
* Abstract method does not have a body (bole toh implementation), they are just declared.
* Abstract method can be declared inside abstract class only.
* Only public or protected modifier is applicable on abstract method.
* Abstract method must be override (defined) by the subclass otherwise the subclass must be declared as abstract.
* Abstract method means WHAT TO DO and and the subclass must provide implementation of that method, mean HOW TO DO.
Real life example:
abstract class Teacher
{
public abstract void countFrom1to100(); // what to do by student
}
class Student extends Teacher // it is must for student to override the method of teacher
{
public void countFrom1to100() // student will count from 1 to 100
{
}
}
note in hindi: teacher apne student ko jo assignment deta hai woh ek tarha se abstract methods hote hain, students ki ye responsibility hoti hai ki woh in assignment ko har haal mai poora kare
# The abstract method of abstract class can be accessed using following guidelines #
step-1: inherit the abstract class
step-2: override the abstract method of super-class
step-3: assign the object of subclass inside the reference-variable of super-class (the polymorphic assignment)
step-4: call the method
example-
abstract class Foo
{
abstract void a(); // this is an abstract method
}
// step-1 --> inherit abstract class
class Test extends Foo
{
// step-2 --> override the abstract method, during overriding dont use abstract
// keyword
void a()
{
}
public static void main(String[] args)
{
// step-3 --> assign the object of subclass inside the reference-variable
// of super-class (the polymorphic assignment)
Foo f = new Test();
// step-4 --> call the method (which was implemented by the subclass)
f.a();
}
}
sr bahut acha smjh aa gya blog se bahut acha likhte ho aap.
ReplyDeleteThanks dear
ReplyDelete