0% found this document useful (0 votes)
5 views12 pages

Java Super Keyword Examples and Concepts

The document contains Java programming exercises demonstrating the use of the 'super' keyword to access parent class variables, methods, and constructors in subclasses. It also includes the creation of abstract classes with concrete implementations for university and government policies. Each exercise is accompanied by code snippets and expected outputs for clarity.

Uploaded by

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

Java Super Keyword Examples and Concepts

The document contains Java programming exercises demonstrating the use of the 'super' keyword to access parent class variables, methods, and constructors in subclasses. It also includes the creation of abstract classes with concrete implementations for university and government policies. Each exercise is accompanied by code snippets and expected outputs for clarity.

Uploaded by

vaibhavi.sengar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA PROGRAMMING (BCA202-2)

JAVA Lab prog 5(B)

BY

Vaibhavi Sengar (24215051), 2 BCA ‘A’

SUBMITTED TO-

Dr. Varuna Gupta

SCHOOL OF SCIENCES
2024-2025
Question: WAP in java using super keyword to access the variable x of
parent class(superParent) in the subclass(superChild) along with its own
variable y.
Code:
package ImpPrograms;

class parent{
int x=10;
}
class child extends parent{
int y=6;
void display()
{
[Link]("Value of x from SuperParent: " + super.x);
[Link]("Value of y from SuperChild: " + y);
}
}
public class access {
public static void main(String args[]) {
child c=new child();
[Link]();
}

}
Output:
Question:
WAP in java using super keyword to access the method superM() of
parent class(superParent) in the subclass(superChild) along with its own
method subM(). and display the value of both the methods.
Code:
package ImpPrograms;

class base{
void method1() {
[Link]("Method of parent class");
}
}
class derived extends base{
void method2() {
[Link]("Method of child class");
}
void display() {
super.method1();
method2();
}
}
public class superr {
public static void main(String args[]) {
derived c=new derived();
[Link]();
}

}
Output:
Question:
WAP in java using super keyword to access the constructor of parent
class(superParent) in th subclass(superChild) along with its own
constructor.
Code:
package ImpPrograms;

class parentt{
parentt(){
[Link]("Hi, I am the parent class constructor");
}
}
class childd extends parentt{
childd(){
super();
[Link]("Hello, I am the child class constructor");
}
}
public class constructor {
public static void main(String args[]) {
childd c=new childd();
}

}
Output:
Question:
Create a abstract class ‘university’ with abstract methods like
‘academic()’ and ‘exam()’. Create a concreate class ‘college’ with
additional method ‘activities()’.
Code:
package ImpPrograms;

abstract class university{


abstract void academic();
abstract void exam();
}
class college extends university{
void academic() {
[Link]("The college is good in academics.");
}
void exam() {
[Link]("The college has two type of exams in a
semester.");
}
void activities() {
[Link]("The college has the best extracurricular
activities in the city.");
}
}
public class abstractuni {
public static void main(String args[]) {
college cg=new college();
[Link]();
[Link]();
[Link]();
}

}
Output:
Question:
Create a abstract class ‘government_policies with abstract methods like
‘Return_File()’ and ‘Pay_Tax()’. Create 2 concreate classes like
‘business_class’ and ‘service_class’ with some additional methods. It
will be compulsory for business class and service class to pay tax and
return file.
Code:
package ImpPrograms;

abstract class government_policies {


abstract void returnFile();
abstract void payTax();
}

class business_class extends government_policies {


void returnFile() {
[Link]("Business class: Filing the return file as per
government policies.");
}
void payTax() {
[Link]("Paying business tax as per govt.");
}
void Investment() {
[Link]("Investing in new projects and business.\n");
}
}

class service_class extends government_policies {


void returnFile() {
[Link]("Service class: Filing the return file as per
individual service rules.");
}
void payTax() {
[Link]("Paying tax based on service income.");
}
void Development() {
[Link]("Upgrading skills.");
}
}

public class paytax {


public static void main(String[] args) {
business_class b=new business_class();
[Link]();
[Link]();
[Link]();
service_class s=new service_class();
[Link]();
[Link]();
[Link]();
}
}

Output:

You might also like