Friday, 4 June 2021

Abstract Class

 




Abstract Class:


  • An abstract class is an incomplete class. An abstract class is defined with the keyword abstract . We cannot create an object of the abstract class because it is not complete.


  • These classes cannot be instantiated and are either partially implemented or not at all implemented. This class contains one or more abstract methods which are simply method declarations without a body.


  • This class is used when common features are shared by all object.


Behaviour is defined by method

Example: 

public void login() // Definition

{

//log in code here

}


If we don’t know the definition of method then we declare the method


public abstract void login(); // Declaration




Summary:

1.  Abstract methods are method which can only be declared, not defined.

2.  Abstract method can only be declared in abstract class

3.  Object of Abstract class can not be created

4.  A class can be declared as abstract if it does not have any abstract method 



Example: 



We have a class which prints configuration with showConfig method at the place of passing object


class iPhone

{

public void showConfig()

{

System.out.println(“2gb, IOS 9.3”);

}

}


public static void show(iPhone obj)

{

obj.showConfig();

}


public static void main(String a[])

{

show(new Iphone());

}





class Samsung

{

public void showConfig()

{

System.out.println(“Octa Core”);

}

}


public static void show(Samsung obj)

{

obj.showConfig();

}


public static void main(String a[])

{

show(new Samsung());

}



At the place of sending cloud account we can abstract a class


abstract class Phone

{

public abstract void showConfig();

}


class iPhone extends Phone

{

public void showconfig()

{

System.out.println(“2gb, IOS 9.3”);

}

}


class Samsung extends Phone

{

public void showconfig()

{

System.out.println(“Octa Core”);


}

}Abstract Class:


  • An abstract class is an incomplete class. An abstract class is defined with the keyword abstract . We cannot create an object of the abstract class because it is not complete.


  • These classes cannot be instantiated and are either partially implemented or not at all implemented. This class contains one or more abstract methods which are simply method declarations without a body.


  • This class is used when common features are shared by all object.


Behaviour is defined by method

Example: 

public void login() // Definition

{

//log in code here

}


If we don’t know the definition of method then we declare the method


public abstract void login(); // Declaration




Summary:

1.  Abstract methods are method which can only be declared, not defined.

2.  Abstract method can only be declared in abstract class

3.  Object of Abstract class can not be created

4.  A class can be declared as abstract if it does not have any abstract method 



Example: 



We have a class which prints configuration with showConfig method at the place of passing object


class iPhone

{

public void showConfig()

{

System.out.println(“2gb, IOS 9.3”);

}

}


public static void show(iPhone obj)

{

obj.showConfig();

}


public static void main(String a[])

{

show(new Iphone());

}





class Samsung

{

public void showConfig()

{

System.out.println(“Octa Core”);

}

}


public static void show(Samsung obj)

{

obj.showConfig();

}


public static void main(String a[])

{

show(new Samsung());

}



At the place of sending cloud account we can abstract a class


abstract class Phone

{

public abstract void showConfig();

}


class iPhone extends Phone

{

public void showconfig()

{

System.out.println(“2gb, IOS 9.3”);

}

}


class Samsung extends Phone

{

public void showconfig()

{

System.out.println(“Octa Core”);


}

}

Skill is Vision

Author & Editor

It is possible to fly without motors, but not without knowledge and skill. The future belongs to those who learn more skills and combine them in creative ways.

0 comments:

Post a Comment