Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

abstract

 
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is use of a non-abstract method in an abstract class?? How can they be accessed?
 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shashank dwivedi wrote:what is use of a non-abstract method in an abstract class?? How can they be accessed?




One way to think about it is... non-abstract methods are default implementations. If a non-abstract class subclass the abstract class, it doesn't have to implement that method (unless of course, it doesn't like the implementation and overrides it).

It is called by using an instance of that class -- but since the class is abstract, that instance is an instance of a subclass.

Henry
 
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget for a moment that the class is abstract. Now ask yourself the same question about those methods: You have a method in a class, how can it be accessed?

......

......

Okay. You have an answer to that? Then the answer is the same if the method is in an abstract class.
 
shashank dwivedi
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Forget for a moment that the class is abstract. Now ask yourself the same question about those methods: You have a method in a class, how can it be accessed?

......

......

Okay. You have an answer to that? Then the answer is the same if the method is in an abstract class.


I got answer of first part but for second part still there is confusion.
If class is not abstract we can call the method by reference of parent class but we can't do the same for an abstract class.
can we some how call methods in parent class using reference of child class extending it?
 
Henry Wong
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shashank dwivedi wrote:If class is not abstract we can call the method by reference of parent class but we can't do the same for an abstract class.



Why not? A subclass IS-A its super class, so you can have a super class reference and assign it to the subclass instance -- and using it is the same regardless of whether the superclass is abstract or not. An abstract class merely means that you can't instantiate it -- it doesn't mean that you can't use a reference of it.

Henry
 
shashank dwivedi
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

shashank dwivedi wrote:what is use of a non-abstract method in an abstract class?? How can they be accessed?




One way to think about it is... non-abstract methods are default implementations. If a non-abstract class subclass the abstract class, it doesn't have to implement that method (unless of course, it doesn't like the implementation and overrides it).

It is called by using an instance of that class -- but since the class is abstract, that instance is an instance of a subclass.

Henry


How is it possible to access method in super class by instance of child class?
 
Paul Clapham
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shashank dwivedi wrote:How is it possible to access method in super class by instance of child class?






 
shashank dwivedi
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shashank dwivedi wrote:

Henry Wong wrote:

shashank dwivedi wrote:what is use of a non-abstract method in an abstract class?? How can they be accessed?




One way to think about it is... non-abstract methods are default implementations. If a non-abstract class subclass the abstract class, it doesn't have to implement that method (unless of course, it doesn't like the implementation and overrides it).

It is called by using an instance of that class -- but since the class is abstract, that instance is an instance of a subclass.

Henry


How is it possible to access method in super class by instance of child class?


abstract class poly
{
public int l,b;
poly(int x,int y)
{
l=x;
b=y;
}
void disp()
{
System.out.print("inside poly");
}
abstract void area();
}
class rect extends poly
{
rect(int x,int y)
{
super(x,y);
}
void area()
{
System.out.println(l*b);
}

@Override
void disp()
{
System.out.println("I am rectangle");
}
}
public class refconcept
{
public static void main(String args[])
{
rect r=new rect(10,10);
poly refer;

r.area();
r.disp();
refer.disp();
}
}

output:
100
I am rectangle
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - variable refer might not have been initialized
at refconcept.main(refconcept.java:41)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)


Why refer.disp() is not working then?
 
Henry Wong
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shashank dwivedi wrote:abstract class poly
{
public int l,b;
poly(int x,int y)
{
l=x;
b=y;
}
void disp()
{
System.out.print("inside poly");
}
abstract void area();
}
class rect extends poly
{
rect(int x,int y)
{
super(x,y);
}
void area()
{
System.out.println(l*b);
}

@Override
void disp()
{
System.out.println("I am rectangle");
}
}
public class refconcept
{
public static void main(String args[])
{
rect r=new rect(10,10);
poly refer;

r.area();
r.disp();
refer.disp();
}
}

output:
100
I am rectangle
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - variable refer might not have been initialized
at refconcept.main(refconcept.java:41)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)


Why refer.disp() is not working then?




The error message is pretty clear. You have a compile error. Fix the compile error -- and never run anything that doesn't compile.

Henry
 
shashank dwivedi
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

shashank dwivedi wrote:abstract class poly
{
public int l,b;
poly(int x,int y)
{
l=x;
b=y;
}
void disp()
{
System.out.print("inside poly");
}
abstract void area();
}
class rect extends poly
{
rect(int x,int y)
{
super(x,y);
}
void area()
{
System.out.println(l*b);
}

@Override
void disp()
{
System.out.println("I am rectangle");
}
}
public class refconcept
{
public static void main(String args[])
{
rect r=new rect(10,10);
poly refer;

r.area();
r.disp();
refer.disp();
}
}

output:
100
I am rectangle
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - variable refer might not have been initialized
at refconcept.main(refconcept.java:41)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)


Why refer.disp() is not working then?




The error message is pretty clear. You have a compile error. Fix the compile error -- and never run anything that doesn't compile.

Henry



I will follow it,But it was necessary to show you what exactly is the problem with code.
 
shashank dwivedi
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shashank dwivedi wrote:

Henry Wong wrote:

shashank dwivedi wrote:abstract class poly
{
public int l,b;
poly(int x,int y)
{
l=x;
b=y;
}
void disp()
{
System.out.print("inside poly");
}
abstract void area();
}
class rect extends poly
{
rect(int x,int y)
{
super(x,y);
}
void area()
{
System.out.println(l*b);
}

@Override
void disp()
{
System.out.println("I am rectangle");
}
}
public class refconcept
{
public static void main(String args[])
{
rect r=new rect(10,10);
poly refer;

r.area();
r.disp();
refer.disp();
}
}

output:
100
I am rectangle
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - variable refer might not have been initialized
at refconcept.main(refconcept.java:41)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)


Why refer.disp() is not working then?




The error message is pretty clear. You have a compile error. Fix the compile error -- and never run anything that doesn't compile.

Henry



I will follow it,But it was necessary to show you what exactly is the problem with code.


Thanks a lot for help sir!
Finally i managed to fix it.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic