0% found this document useful (0 votes)
11 views17 pages

25.abstract - Class - in - Java

This document provides an overview of abstract classes in Java, including their definition, rules, syntax, and examples. It explains the necessity of abstract classes and includes interview questions related to the topic. Additionally, it outlines learning outcomes and references for further reading.

Uploaded by

amitsharma.himcs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views17 pages

25.abstract - Class - in - Java

This document provides an overview of abstract classes in Java, including their definition, rules, syntax, and examples. It explains the necessity of abstract classes and includes interview questions related to the topic. Additionally, it outlines learning outcomes and references for further reading.

Uploaded by

amitsharma.himcs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Program: MCA

Object Oriented Programming with Java


Unit No. 2
Abstract classes in Java
Lecture-4
Mr. Amit Sharma
Assistant Professor, CSA Department, SOET
Outlines
• Prerequisite of topic
• Introduction to Abstract class
• Rules for Abstract class
• Syntax to create an abstract class
• Interview question part-1
• Why we need an abstract class
• Class diagram for abstract class Flyer
• Interview question part-2
• Example of abstract class Animal, and concrete class Dog and Cat
• Important Points to Remember
• Exercise
• Learning outcome
• References
<Page No.>
Prerequisite of topic

• Student should aware from following points


• class fundamentals
• Knowledge of java variables and methods
• Knowledge of Inheritance
Abstract class
• A class which is declared with abstract keyword is known as an abstract
class. It can have abstract and non-abstract methods. It needs to be
extended and its method should be implemented by subclasses.
• When we define a class to be “final”, it cannot be extended. In certain
situation, we want to properties of classes to be always extended and
used. Such classes are called Abstract Classes.
 An Abstract class is a conceptual class.
 An abstract class must be declared with an abstract keyword.
 abstract class contain at least one abstract method
 It can have abstract and non-abstract methods.
 Abstract method is that which has only declaration but no body and The
method body will be defined by its subclass
 Abstract method can never be final and static
 An Abstract class cannot be instantiated – objects cannot be created.
<SELO 1,4> <Reference No.: R1,R2>
Rules of Abstract class

• Concept of Abstract classes derived from the term Abstraction ,and


Abstraction is a process of hiding the implementation details and
showing only functionality to the user.
• A class with one or more abstract methods is automatically abstract
and it cannot be instantiated.
• A class declared abstract, even with no abstract methods can not be
instantiated.
• A subclass of an abstract class can be instantiated if it overrides all
abstract methods by implementation them
• A subclass that does not implement all of the super class abstract
methods is itself abstract; and it cannot be instantiated. process
continues till all the abstract methods get overridden
<SELO 1,4> <Reference No.: R1,R2>
Syntax for Abstract class
abstract class ClassName
{ ...
...
abstract Type MethodName1();

Data Type Method2()
{
// method body
}
}
• When a class contains one or more abstract methods, it should be
declared as abstract class.
• The abstract methods of an abstract class must be defined in its
subclass.
• We cannot declare abstract constructors or abstract static methods.
<Reference No.: R1,R2>
Interview: Abstract class

• Quiz > Can you have a constructor in abstract class ?

• Ans > yes we may have the constructor in the abstract class.

• Quiz>But since the object of abstract class can not be created so


how constructor will call???

• Ans> The constructor of Abstract class can be created and it


doesn’t provide any error but it Calles automatically when the
object of sub class is created..
Why we need an abstract class?
• Lets say we have a class Flyer that has a method Fly, takeoff and
land and the subclasses like Airplane, bird, superman, etc

• Since the flying nature differs from one flyer to another, there is no
point to implement this method in parent class. This is because every
child class must override this method to give its own implementation
details,

• Here, making this method abstract would be the good choice as by


making this method abstract we force all the sub classes to implement
this method( otherwise you will get compilation error), also we need
not to give any implementation to this method in parent class.

<SELO 1,4> <Reference No.: R1,R2>


JAVA

Class diagram for abstract class Flyer

Flyer
+Takeoff()
+Land()
+Fly()

Airplane Bird Superman


+Takeoff() +Takeoff() +Takeoff()
+Land() +Land() +Land()
+Fly() +Fly() +Fly()
+Allow passenger() +BuildNest() +StopBullets()
+Charge money() +Laying eggs() +LeapBuildings()
Interview question
Quection:Why can’t we create the object of an abstract class?

Answer: Because these classes are incomplete classes , they have


abstract methods that have no body so if java allows you to create
object of this class then if someone calls the abstract method using
that object then What would happen? There would be no actual
implementation of the method to invoke.
Also because an object is concrete. An abstract class is like a template,
so you have to extend it and build on it before you can use it.

<Reference No.: R1,R2,R3>


Points to remember

• A class which is not abstract is referred as Concrete classes. so that all


other classes such as airplane,bird and superman are Concrete class.
• An abstract class has no use until unless it is extended by some other
class.
• If you declare an abstract method in a class then you must declare the
class abstract as well.
• We can’t have abstract method in a concrete class and It’s vice versa is
not always true: If a class is not having any abstract method then also it
can be marked as abstract.
• It can have non-abstract method (concrete) as well.
• Always end the method declaration with a semicolon(;).

<Reference No.: R1,R2>


Example-Abstract class
/abstract parent class
abstract class Animal
{
//abstract method
public abstract void sound();
}
//Dog class extends Animal class
public class Dog extends Animal
{
public void sound(){
System.out.println(“ Dog Makes Woof sound");
}
Example- cont…

public class Cat extends Animal


{
public void sound(){
System.out.println(“Cat Makes meow sound");
}
public static void main(String args[]){
Animal dg= new Dog();
Animal cc = new Cat();
dg.sound();
cc.sound();
}
}
Exercise
•Write a java program to create an abstract class shape with abstract
methods area and perimeter. now create following concrete classes
that inherit shape class
•Rectangle
•Circle
•Square

•Provide the implementation of area and perimeter of shape class in all


the subclasses according to the nature of subclasses.
•Required output is area and perimeter of all types of shapes on data
provide by the user.
Learning outcome

• Knowledge about Abstract class


• Rules for Abstract class ,Syntax to create an abstract class
• need an abstract class
• Class diagram for abstract class Flyer
• Example of abstract class Animal, and concrete class Dog and Cat
• Interview question related to abstract classes.

15
Text References
1. The Complete Reference Java By Naughton & Schildt
”, Tata McGraw Hill
2. Core Java by Cay S. Horstmann and Gary Cornell
3. Programming with Java | 6th Edition by E
Balagurusamy
4. Thinking in Java Bruce Eckel President, MindView, Inc.
5. https://www.java67.com/2015/08/top-10-method-overloading-
overriding-interview-questions-answers-java.html

1. 16

You might also like