What is the need of Interfaces and abstract classes? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the need of Interfaces and abstract classes?

5th Jul 2021, 6:55 AM
Atul
Atul - avatar
6 Answers
+ 3
An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces. - infoworld.com Well According to dictionary, "abstract" means existing only as an idea, not as a physical thing विचार या कल्‍पना मात्र में न कि कोई भौतिक वस्‍तु; अमूर्त You can say that if you have an idea and you wanna use it maybe in future, you make that method which you want to be use in future or think to create it as abstract! [ Simple funda ]
5th Jul 2021, 7:02 AM
Abhiyantā
Abhiyantā - avatar
0
you can say a thousand things about abstract classes and interfaces, but the ultimate goal is polymorphism by making available object types regardless of the concrete classes used to instantiate them.
5th Jul 2021, 7:34 AM
Ciro Pellegrino
Ciro Pellegrino - avatar
5th Jul 2021, 7:40 AM
Atul
Atul - avatar
0
/*it all depends on what you wanted to achieve. a possible solution*/ abstract class myclass { abstract void youth(); abstract void old(); public void age() { System.out.println ("brbr"); } public void greet() { age(); System.out.println ("Ogden "); } } class s extends myclass { void youth() { System.out.println ("ch "); } public void age() { System .out .println ("FBI "); } void old() { System.out.println ("thug "); } } public class Program { public static void main(String[] args) { myclass sc=new s(); sc.youth(); sc.old(); sc.age(); sc.greet (); } }
5th Jul 2021, 8:35 AM
Ciro Pellegrino
Ciro Pellegrino - avatar
0
Why I need to change it public? Why it doesn't take public as default?
6th Jul 2021, 5:27 AM
Atul
Atul - avatar
0
/* without modifier means package-private. however you can also leave it without public. the problem remains design. what are you going to do? as a result, the code is written */ public abstract class myclass { abstract void youth(); abstract void old(); void age(){ System.out.println ("brbr"); } void greet(){ age(); System.out.println ("Ogden "); } } class s extends myclass { void youth() { System.out.println ("ch "); } void age() { System .out .println ("FBI "); } void old() { System.out.println ("thug "); } } public class Program { public static void main(String[] args) { myclass sc=new s(); sc.youth(); sc.old(); sc.age(); sc.greet (); } }
6th Jul 2021, 11:35 AM
Ciro Pellegrino
Ciro Pellegrino - avatar