Skip to content

Commit 3a129c6

Browse files
committed
added abstract classes
1 parent e6a1bcd commit 3a129c6

File tree

4 files changed

+209
-0
lines changed

4 files changed

+209
-0
lines changed

Abstract.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.*;
2+
3+
abstract class Person {
4+
String name;
5+
int age;
6+
7+
Person(String name, int age) {
8+
this.name = name;
9+
this.age = age;
10+
}
11+
12+
void doesWork() {
13+
System.out.println("Person does work");
14+
}
15+
16+
public abstract void display();
17+
}
18+
19+
class Student extends Person {
20+
String course;
21+
22+
Student(String name, int age, String course) {
23+
super(name, age);
24+
this.course = course;
25+
}
26+
27+
public void display() {
28+
System.out.println("Name: " + name + " Age: " + age + " Course: " + course);
29+
}
30+
}
31+
32+
public class Abstract {
33+
public static void main(String[] args) {
34+
Student student = new Student("John", 20, "Computer Science");
35+
student.display();
36+
student.doesWork();
37+
}
38+
}

HasA.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import java.util.*;
2+
3+
class Name {
4+
String name;
5+
6+
Name() {
7+
this.name = "";
8+
}
9+
10+
Name(String name) {
11+
this.name = name;
12+
}
13+
14+
public void setName(String name) {
15+
this.name = name;
16+
}
17+
18+
public void printName() {
19+
System.out.println("Name: " + this.name);
20+
}
21+
}
22+
23+
class Address {
24+
String address;
25+
26+
Address() {
27+
this.address = "Mountain View, CA";
28+
}
29+
30+
Address(String address) {
31+
this.address = address;
32+
}
33+
34+
public void setAddress(String address) {
35+
this.address = address;
36+
}
37+
38+
public void printAddress() {
39+
System.out.println("Address: " + this.address);
40+
}
41+
}
42+
43+
class Employee {
44+
String company;
45+
Name name;
46+
Address address;
47+
48+
Employee(String name, String company) {
49+
this.name = new Name(name);
50+
this.address = new Address();
51+
this.company = company;
52+
}
53+
54+
public void printCompany() {
55+
System.out.println("Name "+this.name.name+" works at "+this.company);
56+
System.out.println("Address: "+ this.address.address);
57+
}
58+
}
59+
60+
public class HasA {
61+
public static void main(String[] args) {
62+
Employee emp = new Employee("John Doe", "Google");
63+
emp.printCompany();
64+
65+
if (emp.name instanceof Name) {
66+
System.out.println("'emp.name' is an instance of Name");
67+
}
68+
}
69+
}

Overloading.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import java.util.*;
2+
3+
// Understanding Overloading
4+
// Overloading is a concept in Java where a class can have multiple methods with the same name but different parameters.
5+
// Method or Constructor overloading is a static/compile-time polymorphism.
6+
// The methods must have the same name but different parameters.
7+
// The return type of the methods can be different.
8+
9+
// The methods can be overloaded in the same class or in a subclass.
10+
// The methods can have different access modifiers.
11+
// The methods can throw different exceptions.
12+
// The methods can have different return types.
13+
// The methods can have different number of parameters.
14+
15+
class Calculation {
16+
String operations;
17+
18+
Calculation() {
19+
this.operations = "Add Sub";
20+
}
21+
22+
Calculation(String operations) {
23+
this.operations = operations;
24+
}
25+
26+
public int addition(int a, int b) {
27+
return a + b;
28+
}
29+
30+
public int addition(int a, int b, int c) {
31+
return a + b + c;
32+
}
33+
34+
public void addition(float a, float b) {
35+
System.out.println("Addition: " + (a + b));
36+
return ;
37+
}
38+
39+
public int subtraction(int a, int b) {
40+
return a - b;
41+
}
42+
43+
public int subtraction(int a, int b, int c) {
44+
return a - b - c;
45+
}
46+
47+
public float subtraction(float a, float b) {
48+
return (a - b);
49+
}
50+
}
51+
52+
public class Overloading {
53+
public static void main(String[] args) {
54+
Calculation calc = new Calculation();
55+
56+
System.out.println("Operations: " + calc.operations);
57+
System.out.println("Addition: " + calc.addition(10, 20));
58+
System.out.println("Addition: " + calc.addition(10, 20, 30));
59+
calc.addition(10.5f, 20.5f);
60+
System.out.println("Subtraction: " + calc.subtraction(10, 20));
61+
System.out.println("Subtraction: " + calc.subtraction(10, 20, 30));
62+
System.out.println("Subtraction: " + calc.subtraction(10.5f, 20.5f));
63+
}
64+
}

Overriding.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.*;
2+
3+
// Understanding Overriding
4+
// Overriding is a concept in Java where a subclass can provide a specific implementation of a method that is already provided by its superclass.
5+
// Method overriding is a dynamic/runtime polymorphism.
6+
// The methods must have the same name, same parameters, and same return type.
7+
// The methods can have different access modifiers.
8+
// The methods can throw different exceptions.
9+
10+
class Person {
11+
public void display() {
12+
System.out.println("I am in the Person Class");
13+
}
14+
}
15+
16+
class Student extends Person {
17+
public void display() {
18+
System.out.println("I am in the Student Class");
19+
}
20+
}
21+
22+
class Employee extends Person {
23+
public void display() {
24+
System.out.println("I am in the Employee Class");
25+
}
26+
}
27+
28+
public class Overriding {
29+
public static void main(String[] args) {
30+
Person person = new Person();
31+
Student student = new Student();
32+
Employee employee = new Employee();
33+
34+
person.display();
35+
student.display();
36+
employee.display();
37+
}
38+
}

0 commit comments

Comments
 (0)