Abstract Methods
abstract public displayName();
}
- Abstract keyword can be used with methods to make abstract methods
- Abstract method signifies that method will not have functionality and only the prototype is known
- Method when declared abstract
- No implementation is given
- Only the declaration is provided
- Abstract methods put some compulsion on class that a class which inherit this class must give implementation of all the abstract method or inheriting class must be declared abstract
- There is only the declaration followed by semicolon
- Syntax
- abstract public void method();
- If a class have one or more abstract method declared inside it then class must be declared abstract
abstract public displayName();
}
- An abstract class can't be instantiated i.e an object of abstract class can't be created but the reference variable of abstract class can be created.Because abstract class are not complete class as the method inside abstract class is declared abstract i.e no functionality give so it is worthless to create object of this class
protected int customerId;
protected String customerName;
protected long contactNos[]=new
long[3];
protected Address address;
private static int
counter=1000;
abstract public
void displayCustomerInformation(); // Abstract Method
}
class
RegularCustomer extends Customer{
public
void displayCustomerInformation(){//overridden Abstract method
System.out.println("Customer Id :"+ customerId); // Customer Instance variable
System.out.println("Discount:“+discount);
}
Rule to follow
The following can not be abstract
- Constructor
- Constructor is used to create object and can not be incomplete also not overridden so can not be abstract
- Static Methods
- static methods can not be overridden hence cannot be abstract
- Private Method
- Private methods are only available to its class hence can not be overridden so can not be abstract.
1 . abstract class ParentClass{
protected int parentId;
public ParentClass(){
parentId=1000;
}
abstract public void display();
}
class ChildClass
extends ParentClass{
private int childId;
public ChildClass(){
childId=2000;
}
}
class Demo{
public static void main(String args[]){
ChildClass childOne =
new ChildClass();
}
}
2 . abstract class ParentClass{
protected int parentId;
abstract public ParentClass();
}
class ChildClass
extends ParentClass{
private int childId;
public ChildClass(){
childId=2000;
}
}
class Demo{
public static void main(String args[]){
ChildClass childOne =
new ChildClass();
}
}
3.abstract class ParentClass{
protected int parentId;
public ParentClass(){
parentId=1000;
}
abstract private void display();
}
class ChildClass
extends ParentClass{
private int childId;
public ChildClass(){
childId=2000;
}
public void display(){
System.out.println("Parent
Id:"+parentId+"
Child Id:"+childId);
}
}
class Demo{
public static void main(String args[]){
ChildClass childOne =
new ChildClass();
childOne.display();
}
}
4.abstract class ParentClass{
protected int parentId;
public ParentClass(){
parentId=1000;
}
abstract public void display();
}
class ChildClass
extends ParentClass{
private int childId;
public ChildClass(){
childId=2000;
}
public void display(){
System.out.println("Parent
Id:"+parentId+"
Child Id:"+childId);
}
}
class Demo{
public static void main(String args[]){
ChildClass childOne =
new ChildClass();
childOne.display();
}
}
5.abstract class Example{
public void disp(){
System.out.println("disp in
Example");
}
public abstract void display();
}
class Example1 extends
Example{
public void display(){
System.out.println("display
in Example1");
}
}
class Demo{
public static void main(String args[]){
Example obj=new
Example1();
obj.display();
}
}
6.abstract class Example{
public void disp(){
System.out.println("disp in
Example");
}
public abstract void display();
}
class Example1 extends
Example{
private void display(){
System.out.println("display
in Example1");
}
}
class Demo{
public static void main(String args[]){
Example obj=new
Example1();
obj.display();
}
}
7. abstract class Example{
void disp(){
System.out.println("disp in
Example");
}
public abstract void display(){}
}
abstract class
Example1 extends Example{
public void display1(){
System.out.println("display in Example1");
System.out.println("display in Example1");
}
}
class Example2 extends
Example1{
public void display(){
System.out.println("display
in Example2");
}
}
class Demo{
public static void main(String args[]){
Example2 obj=new Example2();
obj.display();
}
}
Answer by comment and i will share correct answer later
Thank you for reading my blogs
0 comments :
Post a Comment