-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractClass.java
More file actions
51 lines (36 loc) · 1.39 KB
/
AbstractClass.java
File metadata and controls
51 lines (36 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* When there is an abstract method in a class ,that class also should be an abstract class.
* But it is not must to have abstract methods in an abstract class. it can be only have normal methods.
* abstract methods should be declare using inherite concrete class . in the first inherite class all abstract methods should be declared.
* objects cannot be create using abstract classes. but we can use them as reference type .
*/
public class AbstractClass {
public static void main(String[] args){
//Car obj = new Car(); cannot create objects from abstract classes
//WagonR obj = new WagonR();
Car obj = new UpdatedWagonR(); //But can use as reference type
obj.drive();
obj.fly();
obj.playMusic();
obj.x = 22; // variables in abstract classes are normal (Not like in interfaces)
System.out.println(obj.x);
}
}
abstract class Car{
int x = 10; //can initialize variables
public abstract void drive();
public abstract void fly();
public void playMusic(){
System.out.println("Music playing");
}
}
abstract class WagonR extends Car{ //All the abstract methods are not declared in this class. So this is also an abstract class
public void drive(){
System.out.println("Driving");
}
}
class UpdatedWagonR extends WagonR{
public void fly(){
System.out.println("Flying");
}
}