Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

CMJD | Comprehensive Master Java Developer

Abstract Classes and Abstract Methods


01 From Polymorphism 1 Question

class Vehicle{
public void park(){
//
}
}
class Car extends Vehicle{

public void park(){


System.out.println("Car Parking..");
}

}
class Bus extends Vehicle{

public void park(){


System.out.println("Bus Parking..");
}

}
class Van extends Vehicle{

public void park(){


System.out.println("Van Parking..");
}

}
class Demo{

public static void main(String args[]){


Vehicle[] vehicles={new Car(),new Van(),new Car(),new Bus()};
for(Vehicle v1 : vehicles){
v1.park(); //Single interface, many forms==>Polymorphism
}
}
}

Page 1 of 18
CMJD | Comprehensive Master Java Developer

02 From 01 (Illegal)

class Vehicle{
public void park();
}
class Car extends Vehicle{
public void park(){
System.out.println("Car Parking..");
}

}
class Bus extends Vehicle{
public void park(){
System.out.println("Bus Parking..");
}

}
class Van extends Vehicle{
public void park(){
System.out.println("Van Parking..");
}

}
class Demo{
public static void main(String args[]){
Vehicle[] vehicles={new Car(),new Van(),new Car(),new Bus()};
for(Vehicle v1 : vehicles){
v1.park(); //Single interface, many forms==>Polymorphism
}
}
}

03 From 02 (Using abstract classes and abstract methods)

abstract class Vehicle{


abstract public void park();
}
class Car extends Vehicle{
public void park(){
System.out.println("Car Parking..");
}
}

Page 2 of 18
CMJD | Comprehensive Master Java Developer

class Bus extends Vehicle{


public void park(){
System.out.println("Bus Parking..");
}

}
class Van extends Vehicle{
public void park(){
System.out.println("Van Parking..");
}

}
class Demo{
public static void main(String args[]){
Vehicle[] vehicles={new Car(),new Van(),new Car(),new Bus()};
for(Vehicle v1 : vehicles){
v1.park(); //Single interface, many forms==>Polymorphism
}
}
}

04 Case I

class Vehicle{
public void park(); //Illegal
}

05 Case II

class Vehicle{ //Illegal


abstract public void park();
}

06 Case III
abstract class Vehicle{
abstract public void park(){ //Illegal

}
}

Page 3 of 18
CMJD | Comprehensive Master Java Developer

07 Case IV

abstract class Vehicle{


abstract public void park();
public void start(){ //Legal

}
}

08 Case V

abstract class Vehicle{ //Legal


public void park(){
}
public void start(){
}
}

09 Case VI

abstract class Vehicle{ //Legal


abstract public void park();
abstract public void start();
public void stop(){
//body
}
}
class Car extends Vehicle{}
class Jeep extends Vehicle{
public void start(){
//body
}
}
class Van extends Vehicle{
public void park(){
//body
}
}
class Bus extends Vehicle{
public void park(){
//body
}

Page 4 of 18
CMJD | Comprehensive Master Java Developer

public void start(){


//body
}
}
abstract class MB extends Vehicle{
}

10 Case VII

abstract class Vehicle{


abstract public void park();

}
class Demo{
public static void main(String args[]){
Vehicle v1; //Legal
v1=new Vehicle(); //Illegal
}
}

11 Case VIII

abstract class Vehicle{


abstract public void park();
}
class Car extends Vehicle{
public void park(){
System.out.println("Car Parking...");
}
}

class Demo{
public static void main(String args[]){
Vehicle v1=new Car();
v1.park();
}
}

Page 5 of 18
CMJD | Comprehensive Master Java Developer

12 Case IX

abstract class Vehicle{


int year;
abstract String model; //Illegal
abstract public void park();
}

13 Case X

abstract class Vehicle{


int year;
Vehicle(){
//
}
abstract Vehicle(int year); //Illegal

abstract public void park();


}

14 Case XI

abstract class Vehicle{


static abstract public void park(); //Illegal
private abstract public void start(); //Illegal
final abstract public void stop(); //Illegal
}

15 Case XII

class Animal{
public void eat(){
//
}
}
abstract class Lion extends Animal{
abstract public void eat();
}
class Dog extends Animal{} //Legal
class Cat extends Lion{} //Illegal

Page 6 of 18
CMJD | Comprehensive Master Java Developer

Java Interfaces

16.

/*abstract class Vehicle{


abstract public void park();
abstract public void start();
abstract public void stop();
}*/

interface Vehicle{
public void park();
public void start();
public void stop();
}

17.

interface Vehicle{
public void park();
}
//class Car extends Vehicle{} //Wrong
class Car implements Vehicle{
public void park(){
System.out.println("Car Parking..");
}
}
class Demo{
public static void main(String args[]){
Vehicle v1=new Car();
v1.park();
}
}

Page 7 of 18
CMJD | Comprehensive Master Java Developer

18 Case I

interface Vehicle{

}
//class Car extends Vehicle{} // Wrong
class Car implements Vehicle{} // Correct

19 Case II

abstract interface Vehicle{ // Implicitly abstract

20 Case III

interface Vehicle{ // Implicitly abstract


public void park(); // Implicitly abstract
abstract public void start(); // Implicitly abstract
}

21 Case IV

interface Vehicle{
void park(); // Implicitly public
public void start(); //
protected void stop(); // Illegal
}

22 Case V

interface Vehicle{
public void start(){ // Illegal
//body
}
}

Page 8 of 18
CMJD | Comprehensive Master Java Developer

23 Case VI

interface Vehicle{
public void start();
public void park();
}
class Car implements Vehicle{}
class Van implements Vehicle{
public void start(){
//body
}
}
class Jeep implements Vehicle{
public void park(){
//body
}
}
class Bus implements Vehicle{
public void park(){
//body
}
public void start(){
//body
}
}
abstract class MB implements Vehicle{

24 Case VII

interface Vehicle{
public void park();
}
class Demo{
public static void main(String args[]){
Vehicle v1; //Legal
v1=new Vehicle();
}
}

Page 9 of 18
CMJD | Comprehensive Master Java Developer

25 Case VIII

interface Vehicle{
public void park();
}
class Car implements Vehicle{
public void park(){
System.out.println("Car Parking...");
}
}
class Demo{
public static void main(String args[]){
Vehicle v1; //Legal
v1=new Car();
}
}
//Car IS-A Vehicle
//Car - Subclass
//Vehicle->Super interface

26 Case IX

interface Vehicle{
int year=1999; //implicitly final, static and public
public void park();
}
class Demo{
public static void main(String args[]){
System.out.println(Vehicle.year);//prints 1999
Vehicle.year=2000; //Illegal, final
}
}

27 Case X

interface Vehicle{
int year=1999;
Vehicle(){ //Illegal
}
public void park();
}

Page 10 of 18
CMJD | Comprehensive Master Java Developer

28 From Q03 (Using java “interfaces”)

interface Vehicle{
public void park();
}
class Car implements Vehicle{
public void park(){
System.out.println("Car Parking..");
}
}
class Bus implements Vehicle{
public void park(){
System.out.println("Bus Parking..");
}
}
class Van implements Vehicle{
public void park(){
System.out.println("Van Parking..");
}
}
class Demo{
public static void main(String args[]){
Vehicle[] vehicles={new Car(),new Van(),new Car(),new Bus()};
for(Vehicle v1 : vehicles){
v1.park(); //Single interface, many forms==>Polymorphism
}
}
}

29. Blueprint of a Class

interface Date{
public void setDate(int year, int month, int day);
public void setYear(int year);
public void setMonth(int month);
public void setDay(int day);
public int getYear();
public int getMonth();
public int getDay();
public void printDate();
}

Page 11 of 18
CMJD | Comprehensive Master Java Developer

class DateImpl implements Date{


private int year;
private int month;
private int day;
DateImpl(){

}
public void setDate(int year, int month, int day){
setYear(year);
setMonth(month);
setDay(day);
}
public void setYear(int year){
this.year=year;
}
public void setMonth(int month){
this.month=month;
}
public void setDay(int day){
this.day=day;
}
public int getYear(){
return year;
}
public int getMonth(){
return month;
}
public int getDay(){
return day;
}

public void printDate(){


System.out.println(toString()); //this
}
public String toString (){
return year+"-"+month+"-"+day;
}
public boolean equals(Object obj){
DateImpl d1=(DateImpl)obj;
return this.year==d1.year && this.month==d1.month && this.day==d1.day;
}
}

Page 12 of 18
CMJD | Comprehensive Master Java Developer

class Demo{
public static void main(String args[]){
Date d1=new DateImpl();
d1.setDate(1999,12,31);
d1.printDate();
}
}

30 Case XI

class Animal{
}
interface Lion{
}
class Dog extends Animal implements Lion{ //Legal->Multiple inheritance
}
//Dog IS-A Lion
//Dog IS-AN Animal

Page 13 of 18
CMJD | Comprehensive Master Java Developer

31.

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
interface WaterLevelObserver{
public void update(int waterLevel);
}
class SMSWindow extends JFrame implements WaterLevelObserver{
private JLabel smsLabel;
SMSWindow(){
setSize(300,300);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setTitle("SMS Window");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
//-----------------------------------------
smsLabel=new JLabel("0");
smsLabel.setFont(new Font("",1,25));
add(smsLabel);
setVisible(true);
}
public void update(int waterLevel){
smsLabel.setText("Sending SMS : "+waterLevel);
}
}
class DisplayWindow extends JFrame implements WaterLevelObserver{
private JLabel displayLabel;
DisplayWindow(){
setSize(300,300);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setTitle("Display Window");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
//-----------------------------------------
displayLabel=new JLabel("0");
displayLabel.setFont(new Font("",1,25));
add(displayLabel);
setVisible(true);
}

Page 14 of 18
CMJD | Comprehensive Master Java Developer

public void update(int waterLevel){


displayLabel.setText(waterLevel+""); //Integer.toString(waterLevel)
}
}
class AlarmWindow extends JFrame implements WaterLevelObserver{
private JLabel alarmLabel;
AlarmWindow(){
setSize(300,300);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setTitle("Alarm Window");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
//-----------------------------------------
alarmLabel=new JLabel("OFF");
alarmLabel.setFont(new Font("",1,25));
add(alarmLabel);
setVisible(true);
}
public void update(int waterLevel){
alarmLabel.setText(waterLevel>=50 ? "ON":"OFF");
}
}
class SplitterWindow extends JFrame implements WaterLevelObserver{
private JLabel splitterLabel;
SplitterWindow(){
setSize(300,300);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setTitle("Splitter Window");
setLocationRelativeTo(null);
setLayout(new FlowLayout());
//-----------------------------------------
splitterLabel=new JLabel("OFF");
splitterLabel.setFont(new Font("",1,25));
add(splitterLabel);
setVisible(true);
}
public void update(int waterLevel){
splitterLabel.setText(waterLevel>=75 ? "Splitter ON":"Splitter OFF");
}
}
class WaterTankWindow extends JFrame{
private WaterTankController waterTankController;

Page 15 of 18
CMJD | Comprehensive Master Java Developer

private JSlider waterLevelSlider;


WaterTankWindow(WaterTankController waterTankController){
setSize(300,300);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setTitle("WaterTank Window");
setLocationRelativeTo(null);
setLayout(new FlowLayout());

this.waterTankController=waterTankController;
//-----------------------------------------
waterLevelSlider=new JSlider(JSlider.VERTICAL,0,100,50);
waterLevelSlider.setFont(new Font("",1,25));
waterLevelSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent ce) {
int waterLevel=waterLevelSlider.getValue();
waterTankController.setWaterLevel(waterLevel);
}
});
add(waterLevelSlider);
}
}
class WaterTankController{
private WaterLevelObserver[] observerArray=new WaterLevelObserver[0];
private int waterLevel;
public void addWaterLevelObserver(WaterLevelObserver waterLevelObserver){
WaterLevelObserver[] temp=new WaterLevelObserver[observerArray.length+1];
for(int i=0; i<observerArray.length; i++){
temp[i]=observerArray[i];
}
temp[observerArray.length]=waterLevelObserver;
observerArray=temp;
}
public void setWaterLevel(int waterLevel){
if(this.waterLevel!=waterLevel){
this.waterLevel=waterLevel;
notifyObservers();
}
}
public void notifyObservers(){
for(WaterLevelObserver ob : observerArray){
ob.update(waterLevel);

Page 16 of 18
CMJD | Comprehensive Master Java Developer

}
}
}
class Demo{

public static void main(String args[]){


WaterTankController waterTankController=new WaterTankController();
waterTankController.addWaterLevelObserver(new AlarmWindow());
waterTankController.addWaterLevelObserver(new AlarmWindow());
waterTankController.addWaterLevelObserver(new DisplayWindow());
waterTankController.addWaterLevelObserver(new SplitterWindow());
waterTankController.addWaterLevelObserver(new SplitterWindow());
waterTankController.addWaterLevelObserver(new SMSWindow());
new WaterTankWindow(waterTankController).setVisible(true);
}
}

32. Case XII

interface Lion{}
interface Fox extends Lion{}
interface Cat{}
interface Dog extends Lion,Cat{} // Legal

33. Case XIII

class Lion{}
interface Cat{}
interface Dog{}

class Man extends Lion implements Cat,Dog{} // Legal

34. Case XIV

class Lion{}
interface Cat extends Lion{} // Illegal
interface Dog implements Lion{} // Illegal

Page 17 of 18
CMJD | Comprehensive Master Java Developer

35. Case XV

interface Vehicle{
public void park();

default public void start(){ //Legal, since JDK1.8


System.out.println("Default Implementation");
}
}
class Car implements Vehicle{
public void park(){
System.out.println("Car Parking..");
}
}
class Demo{
public static void main(String args[]){
Vehicle v1=new Car();
v1.park();
v1.start();
}
}

36. Case XVI

interface Vehicle{
public void park();

default public void start(){ //Legal, since JDK1.8


System.out.println("Default Implementation");
}
public static void calculate(){ //Legal
System.out.println("Static implementation..");
}
}
interface Demo{
public static void main(String args[]){
Vehicle.calculate();
}
}

Page 18 of 18

You might also like