+2 votes
163 views
We can't create object for abstract class my professor said, now my question without object creation how the abstract class invoke???
asked Jan 11, 2016 in Computer Science - IT by deepika | 163 views
yes you cannot create an object and in order to call the abstract class you need to create a subclass :public class MyClass extends AbstractClass

2 Answers

+3 votes
Selected Ans.

hi,

    i will explain something about abstract class and methods.

What is abstract class?

-> Abstract class is a class that is declared abstract keyword, it may or may not include abstract methods.

-> we cannot create object for abstract class, but we can call abstract class method using derived class objects.

-> When an abstract class is inherited, the child usually provides implementations for all of the abstract methods in its parent class.

simple example to understand:

Consider college id card, suppose the college give chance to prepare id card to student itself na, all students are designed id card with different details..so only college administration designed id card with same details…

in this way consider id card as a base class and students are derived class…we must need to use the methods in base class…

hence, when the situation arise the derived class must need to use (implement) base class method na,that time better to use abstract…

Example program...

import java.util.*;
abstract class common_id
{
    abstract void idno();
    abstract void sname();
    abstract void dept();
}
class student extends common_id
{ 
    int id;
    String name,dep;
    Scanner sc=new Scanner(System.in);
    void idno() 
    {
    System.out.println("enter your idno");
    id=sc.nextInt();
        
    }

    void sname() 
    {
        System.out.println("enter your name");
        name=sc.next();
        
    }

    void dept() 
    {
        System.out.println("enter your dep");
        dep=sc.next();    
        
    }
void local()
{
System.out.println("id="+id);
System.out.println("name="+name);
System.out.println("department="+dep);
    
}

}
public class studex 
{
public static void main(String args[])
{
student s=new student();
s.idno();
s.sname();
s.dept();
s.local();

}
}

output...

enter your idno :1001
enter your name : deepika
enter your dep : CSE
id=1001
name= deepika
department= CSE

thank you

 

answered Jan 12, 2016 by gypsiii
selected Feb 18, 2016 by deepika
gypsiii .doin great in Java :)  +1
tanq....androidlover...
"Consider college id card, suppose the college give chance to prepare id card to student itself na, all students are designed id card with different details..so only college administration designed id card with same details…" How does student design with different details ?
have possible? some student mention mobile num,some don't mention mobile num,some mention blood group,some don't.........
–1 vote
Abstract classes are classes that contain one or more abstract methods. By using abstract classes, we can inherit the implementation of other (non-abstract) methods.For example:suppose we can take the example of animal.Animal can do different things but there is something that is common among all the animals i.e. eating and sleeping, might be possible these activities are done in different ways .When the operation is performed in different ways then it forces sub classes to provide custom implementation.Abstract keyword is used to denote both an abstract method and abstract class.
answered Jan 12, 2016 by Shikha tyagi
does not answer the question by user !