Week-2 (OOP with Java -II)
CE204 Object-Oriented Programming¶
Week-2 (OOP with Java-II)¶
Spring Semester, 2021-2022¶
Download DOC-PDF, DOC-DOCX, SLIDE, PPTX,
OOP with Java-II¶
Outline (1)¶
- Java super Keyword
- Java final Keyword
- Java Polymorphism / Encapsulation
- Java Method Overriding
- Java Nested Inner Class
- Java Static Class
- Java Anonymous Class
Outline (2)¶
- Java Enums / Enum-Constructor / Enum-String
- Java Abstract Class
- Java Object Class
- Java Forms of Inheritance
- Java Benefits and Costs of Inheritance
- Java Packages
- Java Access Protection in Packages
Java super keyword¶
Java super keyword¶
- In java,
super
is a keyword used to refers to the parent class object. - The
super
keyword came into existence to solve the naming conflicts in the inheritance. - When both parent class and child class have members with the same name,
- then the super keyword is used to refer to the parent class version.
Java super keyword¶
- In another word, The super keyword in Java is used in subclasses to access superclass members (attributes, constructors and methods).
Java super keyword¶
- In java, the super keyword is used for the following purposes.
- To refer parent class data members
- To refer parent class methods
- To call parent class constructor
Java super keyword¶
- To call methods of the superclass that is overridden in the subclass.
- To access attributes (fields) of the superclass if both superclass and subclass have attributes with the same name.
- To explicitly call superclass no-arg (default) or parameterized constructor from the subclass constructor.
Java super keyword¶
- The super keyword is used inside the child class only.
super to refer parent class data members¶
- When both parent class and child class have data members with the same name,
- then the super keyword is used to refer to the parent class data member from child class.
super to refer parent class data members¶
super to refer parent class data members¶
super to refer parent class method¶
- When both parent class and child class have method with the same name,
- then the super keyword is used to refer to the parent class method from child class.
super to refer parent class method¶
class ParentClass{
int num1 = 10;
void showData() {
System.out.println("\nInside the ParentClass showData method");
System.out.println("ChildClass num = " + num1);
}
}
super to refer parent class method¶
class ChildClass extends ParentClass{
int num2 = 20;
void showData() {
System.out.println("\nInside the ChildClass showData method");
System.out.println("ChildClass num = " + num2);
super.showData();
}
}
super to refer parent class method¶
public class SuperKeywordExample {
public static void main(String[] args) {
ChildClass obj = new ChildClass();
obj.showData();
//super.showData(); // super can't be used here
}
}
super to call parent class constructor¶
- When an object of child class is created, it automatically calls the parent class default-constructor before it's own.
- But, the parameterized constructor of parent class must be called explicitly using the super keyword inside the child class constructor.
super to call parent class constructor¶
super to call parent class constructor¶
super to call parent class constructor¶
super to call parent class constructor¶
- To call the parameterized constructor of the parent class,
- the super keyword must be the first statement inside the child class constructor,
- and we must pass the parameter values.
Access Overridden Methods of the superclass¶
- If methods with the same name are defined in both superclass and subclass, the method in the subclass overrides the method in the superclass. This is called method overriding.
Example 1: Method overriding¶
Example 1: Method overriding¶
Example 1: Method overriding¶
Example 1: Method overriding¶
In this example, by making an object dog1 of Dog class, we can call its method printMessage() which then executes the display() statement.
Since display() is defined in both the classes, the method of subclass Dog overrides the method of superclass Animal. Hence, the display() of the subclass is called.
Example 1: Method overriding¶
What if the overridden method of the superclass has to be called?¶
- We use super.display() if the overridden method display() of superclass Animal needs to be called.
Example 2: super to Call Superclass Method¶
Example 2: super to Call Superclass Method¶
Example 2: super to Call Superclass Method¶
Example 2: super to Call Superclass Method¶
Access Attributes of the Superclass¶
- The superclass and subclass can have attributes with the same name.
- We use the super keyword to access the attribute of the superclass.
Example 3: Access superclass attribute¶
Example 3: Access superclass attribute¶
Example 3: Access superclass attribute¶
- In this example, we have defined the same instance field
type
in both the superclassAnimal
and the subclassDog
. - We then created an object
dog1
of the Dog class. Then, theprintType()
method is called using this object. - Inside the
printType()
function,type
refers to the attribute of the subclassDog
.super.type
refers to the attribute of the superclass Animal.
Use of super() to access superclass constructor¶
- As we know, when an object of a class is created, its default constructor is automatically called.
- To explicitly call the superclass constructor from the subclass constructor, we use
super()
. It's a special form of the super keyword. super()
can be used only inside the subclass constructor and must be the first statement.
Example 4: Use of super()¶
Example 4: Use of super()¶
Example 4: Use of super()¶
Example 4: Use of super()¶
-
when an object dog1 of Dog class is created, it automatically calls the default or no-arg constructor of that class.
-
Inside the subclass constructor, the super() statement calls the constructor of the superclass and executes the statements inside it. Hence, we get the output I am an animal.
Example 4: Use of super()¶
The flow of the program then returns back to the subclass constructor and executes the remaining statements. Thus, I am a dog will be printed.
However, using super() is not compulsory. Even if super() is not used in the subclass constructor, the compiler implicitly calls the default constructor of the superclass.
Example 4: Use of super()¶
- So, why use redundant code if the compiler automatically invokes super()?
-
It is required if the parameterized constructor (a constructor that takes arguments) of the superclass has to be called from the subclass constructor.
-
The parameterized super() must always be the first statement
- in the body of the constructor of the subclass,
- otherwise, we get a compilation error.
Example 5: Call Parameterized Constructor Using super()¶
Example 5: Call Parameterized Constructor Using super()¶
Example 5: Call Parameterized Constructor Using super()¶
Example 5: Call Parameterized Constructor Using super()¶
If a parameterized constructor has to be called, we need to explicitly define it in the subclass constructor.
Example 5: Call Parameterized Constructor Using super()¶
Note that in the above example, we explicitly called the parameterized constructor super("Animal"). The compiler does not call the default constructor of the superclass in this case.
Java final keyword¶
Java final keyword¶
- In java, the final is a keyword and it is used with the following things.
- With variable (to create constant)
- With method (to avoid method overriding)
- With class (to avoid inheritance)
Java final restrictions¶
- the final variable cannot be reinitialized with another value
- the final method cannot be overridden
- the final class cannot be extended
final with variables¶
- When a variable defined with the final keyword,
- it becomes a constant, and
- it does not allow us to modify the value.
- The variable defined with the final keyword allows only a one-time assignment,
- once a value assigned to it,
- never allows us to change it again.
final with variables example-1¶
final with variables example-2¶
final with variables recommendation¶
- It is recommended to use uppercase to declare final variables in Java.
final with methods¶
- When a method defined with the final keyword,
- it does not allow it to override.
- The final method extends to the child class,
- but the child class can not override or re-define it.
- It must be used as it has implemented in the parent class.
final with methods example-1¶
final with methods example-1¶
final with methods example-1¶
final with methods example-2¶
final with class¶
- When a class defined with final keyword, it can not be extended by any other class.
final with class example-1¶
final with class example-1¶
final with class example-1¶
final with class example-2¶
Java Polymorphism¶
Java Polymorphism¶
- The polymorphism is the process of defining same method with different implementation. That means creating multiple methods with different behaviors.
- In java, polymorphism implemented using
- method overloading and
- method overriding.
Ad hoc polymorphism¶
- The ad hoc polymorphism is a technique used to define
- the same method with different implementations and
- different arguments.
- In a java programming language, ad hoc polymorphism carried out with
- a method overloading concept.
Ad hoc polymorphism¶
- In ad hoc polymorphism the method binding happens at the time of compilation.
- Ad hoc polymorphism is also known as compile-time polymorphism.
- Every function call binded with the respective overloaded method based on the arguments.
Ad hoc polymorphism¶
- The ad hoc polymorphism implemented within the class only.
Ad hoc polymorphism example-1¶
Ad hoc polymorphism example-1¶
Pure polymorphism¶
- The pure polymorphism is a technique used to define the same method with the same arguments but different implementations.
- In a java programming language, pure polymorphism carried out with
- a method overriding concept.
Pure polymorphism¶
- In pure polymorphism, the method binding happens at run time.
- Pure polymorphism is also known as run-time polymorphism.
-
Every function call binding with the respective overridden method based on the object reference.
-
When a child class has a definition for a member function of the parent class,
- the parent class function is said to be overridden.
Pure polymorphism¶
- The pure polymorphism implemented in the inheritance concept only.
Pure polymorphism example-1¶
Pure polymorphism example-1¶
Pure polymorphism example-1¶
Java Method Overriding¶
- During inheritance in Java, if the same method is present in both the superclass and the subclass.
- Then, the method in the subclass overrides the same method in the superclass. This is called method overriding.
Polymorphism using method overriding example-2¶
¶
Polymorphism using method overriding example-2¶
Polymorphism using method overriding example-2¶
Java Method Overloading¶
In a Java class, we can create methods with the same name if they differ in parameters. For example
This is known as method overloading in Java. Here, the same method will perform different operations based on the parameter.
Polymorphism using method overloading example-3¶
Polymorphism using method overloading example-3¶
Polymorphic Variables¶
- A variable is called polymorphic if it refers to different values under different conditions.
- Object variables (instance variables) represent the behavior of polymorphic variables in Java.
- It is because object variables of a class can refer to objects of its class as well as objects of its subclasses.
Polymorphic Variables Example-1¶
Polymorphic Variables Example-1¶
Polymorphic Variables Example-1¶
Java Encapsulation¶
Java Encapsulation¶
- It prevents outer classes from accessing and changing fields and methods of a class. This also helps to achieve data hiding
Java Encapsulation Example¶
Java Encapsulation Example¶
Why Encapsulation?¶
- In Java, encapsulation helps us to keep
- related
- fields and
- methods together,
- which makes our code cleaner and easy to read.
Why Encapsulation?¶
- It helps to control the values of our data fields
Why Encapsulation?¶
- The getter and setter methods provide
- read-only or
- write-only
- access to our class fields
¶
Why Encapsulation?¶
- It helps to decouple components of a system.
- For example,
- we can encapsulate code into multiple bundles.
- These decoupled components (bundle)
- can be developed,
- tested, and
- debugged independently and concurrently.
- And, any changes in a particular component
- do not have any effect on other components.
Why Encapsulation?¶
- We can also achieve data hiding using encapsulation.
- In the next example,
- if we change the length and breadth variable into private,
- then the access to these fields is restricted.
- And, they are kept hidden from outer classes.
- This is called data hiding.
Why Encapsulation?¶
Why Encapsulation?¶
Data Hiding¶
-
Data hiding is a way of restricting the access of our data members by hiding the implementation details.
-
Encapsulation also provides a way for data hiding.
-
We can use access modifiers to achieve data hiding
Data hiding using the private specifier example¶
- Making
age
private allowed us to restrict unauthorized access from outside the class. This is data hiding.
Data hiding using the private specifier example¶
Data hiding using the private specifier example¶
Java Method Overriding¶
Java Method Overriding¶
- The method overriding is the process of re-defining a method in a child class that is already defined in the parent class.
- When both parent and child classes have the same method, then that method is said to be the overriding method.
- The method overriding enables the child class to change the implementation of the method which aquired from parent class according to its requirement.
Java Method Overriding¶
The method overriding is also known as - dynamic method dispatch or - run time polymorphism or - pure polymorphism.
Java Method Overriding Example¶
Java Method Overriding Example¶
Java Method Overriding Example¶
Rules for method overriding¶
While overriding a method, we must follow the below list of rules.
- Static methods can not be overridden.
- Final methods can not be overridden.
- Private methods can not be overridden.
- Constructor can not be overridden.
- An abstract method must be overridden.
- Use super keyword to invoke overridden method from child class.
Rules for method overriding¶
- The return type of the overriding method must be same as the parent has it.
- The access specifier of the overriding method can be changed, but the visibility must increase but not decrease. For example, a protected method in the parent class can be made public, but not private, in the child class.
Rules for method overriding¶
- If the overridden method does not throw an exception in the parent class, then the child class overriding method can only throw the unchecked exception, throwing a checked exception is not allowed.
- If the parent class overridden method does throw an exception, then the child class overriding method can only throw the same, or subclass exception, or it may not throw any exception.
Method Overriding Example¶
Method Overriding Example¶
-
annotations are the metadata that we used to provide information to the compiler
-
It is not mandatory to use @Override. However, when we use this, the method should follow all the rules of overriding. Otherwise, the compiler will generate an error.
Method Overriding Example¶
super Keyword in Java Overriding¶
- Can we access the method of the superclass after overriding?
- The answer is Yes. To access the method of the superclass from the subclass, we use the super keyword
Use of super Keyword Example¶
Use of super Keyword Example¶
-
In the above example, the subclass Dog overrides the method displayInfo() of the superclass Animal.
-
When we call the method displayInfo() using the d1 object of the Dog subclass, the method inside the Dog subclass is called; the method inside the superclass is not called
-
Inside displayInfo() of the Dog subclass, we have used super.displayInfo() to call displayInfo() of the superclass.
Use of super Keyword Example¶
-
note that constructors in Java are not inherited. Hence, there is no such thing as constructor overriding in Java.
-
However, we can call the constructor of the superclass from its subclasses. For that, we use super()
Access Specifiers in Method Overriding¶
-
The same method declared in the superclass and its subclasses can have different access specifiers. However, there is a restriction.
-
We can only use those access specifiers in subclasses that provide larger access than the access specifier of the superclass. For example,
-
Suppose, a method myClass() in the superclass is declared protected. Then, the same method myClass() in the subclass can be either public or protected, but not private.
Access Specifier in Overriding Example¶
Access Specifier in Overriding Example¶
-
In the above example, the subclass Dog overrides the method displayInfo() of the superclass Animal.
-
Whenever we call displayInfo() using the d1 (object of the subclass), the method inside the subclass is called.
-
Notice that, the displayInfo() is declared protected in the Animal superclass. The same method has the public access specifier in the Dog subclass.
- This is possible because the public provides larger access than the protected.
Overriding Abstract Methods¶
- In Java, abstract classes are created to be the superclass of other classes.
- And, if a class contains an abstract method,
- it is mandatory to override it.
Java Nested and Inner Class¶
Java Nested and Inner Class¶
- In Java, you can define a class within another class.
- Such class is known as nested class
Java Nested and Inner Class¶
- There are two types of nested classes you can create in Java.
- Non-static nested class (inner class)
- Static nested class
Non-Static Nested Class (Inner Class)¶
- A non-static nested class is a class within another class.
-
It has access to members of the enclosing class (outer class).
- It is commonly known as inner class.
-
Since the inner class exists within the outer class,
- you must instantiate the outer class first,
- in order to instantiate the inner class.
Non-Static Nested Class (Inner Class) Example¶
Non-Static Nested Class (Inner Class) Example¶
Non-Static Nested Class (Inner Class) Example¶
- In the example program, there are two nested classes:
- Processor and RAM inside the outer class:
- CPU.
- We can declare the inner class as protected.
-
Hence, we have declared the RAM class as protected.
-
Inside the Main class,
- we first created an instance of an outer class CPU named cpu.
- Using the instance of the outer class, we then created objects of inner classes
Accessing Members of Outer Class within Inner Class¶
- We can access the members of the outer class by using this keyword
Accessing Members of Outer Class within Inner Class Example¶
Accessing Members of Outer Class within Inner Class Example¶
Accessing Members of Outer Class within Inner Class Example¶
Accessing Members of Outer Class within Inner Class Example¶
- In the example program, we have the inner class named
- Engine inside the outer class Car. Here, notice the line,
- We are using
this
keyword to access thecarType
variable of the outer class. - You may have noticed that instead of using
this.carType
we have usedCar.this.carType
Accessing Members of Outer Class within Inner Class Example¶
- It is because if we had not mentioned the name of the outer class Car,
-
then this keyword will represent the member inside the inner class.
-
Similarly, we are also accessing the method of the outer class from the inner class.
- It is important to note that, although the
getCarName()
is aprivate
method, we are able to access it from the inner class.
Static Nested Class¶
- In Java, we can also define a static class inside another class.
- Such class is known as static nested class.
-
Static nested classes are not called static inner classes.
-
Unlike inner class, a static nested class cannot access the member variables of the outer class.
- It is because the static nested class doesn't require you to create an instance of the outer class.
- Here, we are creating an object of the static nested class by simply using the class name of the outer class.
- Hence, the outer class cannot be referenced using
OuterClass.this
.
Static Inner Class Example¶
Static Inner Class Example¶
- In the above program, we have created a static class named USB inside the class MotherBoard. Notice the line,
-
Here, we are creating an object of USB using the name of the outer class.
-
Now, let's see what would happen if you try to access the members of the outer class:
Accessing members of Outer class inside Static Inner Class Example¶
Accessing members of Outer class inside Static Inner Class Example¶
Accessing members of Outer class inside Static Inner Class Example¶
- When we try to run the program, we will get an error:
Motherboard
stored in Motherboard.this
.
Key Points to Remember¶
- Java treats the inner class as a regular member of a class. They are just like methods and variables declared inside a class.
- Since inner classes are members of the outer class, you can apply any access modifiers like private, protected to your inner class which is not possible in normal classes.
- Since the nested class is a member of its enclosing outer class, you can use the dot (.) notation to access the nested class and its members.
- Using the nested class will make your code more readable and provide better encapsulation.
- Non-static nested classes (inner classes) have access to other members of the outer/enclosing class, even if they are declared private.
Java Nested Static Class¶
Java Nested Static Class¶
- we can have a class inside another class in Java. Such classes are known as nested classes. In Java, nested classes are of two types:
- Nested non-static class (Inner class)
- Nested static class.
Java Nested Static Class¶
- We use the keyword static to make our nested class static.
- Note: In Java, only nested classes are allowed to be static.
- Like regular classes, static nested classes can include both static and non-static fields and methods. For example,
Static Nested Class Example¶
Static Nested Class Example¶
Static Nested Class Example¶
- In the example program, we have two nested class
Mammal
andReptile
inside a classAnimal
. - To create an object of the non-static class Reptile, we have used
- To create an object of the static class Mammal, we have used
Accessing Members of Outer Class¶
- In Java, static nested classes are associated with the outer class.
- This is why static nested classes can only access the class members (static fields and methods) of the outer class.
Accessing Non-static members Example¶
Accessing Non-static members Example¶
Accessing Non-static members Example¶
-
In the example, we have created a non-static method
eat()
inside the class Animal. -
Now, if we try to access
eat()
using the objectmammal
, the compiler shows an error. -
It is because
mammal
is an object of a static class and we cannot access non-static methods from static classes.
Static Top-level Class¶
- only nested classes can be static.
- We cannot have static top-level classes.
Static Top-level Class¶
- if we try to make a top-level class static.
Static Top-level Class¶
Main.java:1: error: modifier static not allowed here
static class Animal {
^
1 error
compiler exit status 1
- In the example, we have tried to create a static class Animal.
- Since Java doesn’t allow static top-level class,
- we will get an error.
Java Anonymous Class¶
Java Anonymous Class¶
- In Java, a class can contain another class known as nested class. It's possible to create a nested class without giving any name.
- A nested class that doesn't have any name is known as an anonymous class.
- An anonymous class must be defined inside another class. Hence, it is also known as an anonymous inner class. Its syntax is:
Java Anonymous Class¶
- Anonymous classes usually extend subclasses or implement interfaces.
- Here, Type can be
- a superclass that an anonymous class extends
- an interface that an anonymous class implements
- The above code creates an object, object1, of an anonymous class at runtime.
- Note: Anonymous classes are defined inside an expression. So, the semicolon is used at the end of anonymous classes to indicate the end of the expression.
Anonymous Class Extending a Class Example¶
Anonymous Class Extending a Class Example¶
Anonymous Class Extending a Class Example¶
- In the example, we have created a class
Polygon
. It has a single methoddisplay()
. - We then created an anonymous class that extends the class Polygon and overrides the
display()
method. - When we run the program, an object
p1
of the anonymous class is created. - The object then calls the
display()
method of the anonymous class.
Anonymous Class Implementing an Interface Example¶
Anonymous Class Implementing an Interface Example¶
- In the example, we have created an anonymous class that implements the Polygon interface.
Advantages of Anonymous Classes¶
- In anonymous classes, objects are created whenever they are required.
- That is, objects are created to perform some specific tasks. For example,
- Here, an object of the anonymous class is created dynamically when we need to override the display() method.
- Anonymous classes also help us to make our code concise.
Java enums¶
Java enums¶
- In Java, an enum (short for enumeration) is a type that has a fixed set of constant values. We use the
enum
keyword to declare enums. For example,
SMALL
, MEDIUM
, LARGE
, and EXTRALARGE
.
- These values inside the braces are called enum constants (values).
- Note: The enum constants are usually represented in uppercase.
Java Enum Example¶
Java Enum Example¶
we use the enum name to access the constant values.
Also, we can create variables of enum types. For example
- Here, pizzaSize is a variable of the Size type. It can only be assigned with 4 values.
Java Enum with the switch statement example¶
Java Enum with the switch statement example¶
Java Enum with the switch statement example¶
Java Enum with the switch statement example¶
- In the example, we have created an enum type
Size
. - We then declared a variablepizzaSize
of theSize
type. - Here, the variable
pizzaSize
can only be assigned with 4 values (SMALL, MEDIUM, LARGE, EXTRALARGE
). - Notice the statement,
- It will call the
Test()
constructor inside theTest
class. Now, the variable pizzaSize is assigned with theMEDIUM
constant. - Based on the value, one of the cases of the switch case statement is executed.
Enum Class in Java¶
- In Java, enum types are considered to be a special type of class.
- It was introduced with the release of Java 5.
- An enum class can include methods and fields just like regular classes.
- When we create an enum class, the compiler will create instances (objects) of each enum constants.
- Also, all enum constant is always public static final by default.
Java Enum Class Example¶
Java Enum Class Example¶
Java Enum Class Example¶
- In the example, we have created an enum class Size. It has four constants SMALL, MEDIUM, LARGE and EXTRALARGE.
- Since Size is an enum class, the compiler automatically creates instances for each enum constants.
- Here inside the main() method, we have used the instance SMALL to call the getSize() method.
- Note: Like regular classes, an enum class also may include constructors
Methods of Java Enum Class¶
- There are some predefined methods in enum classes that are readily available for use.
Methods of Java Enum Class¶
Java Enum ordinal()¶
- The ordinal() method returns the position of an enum constant. For example,
Methods of Java Enum Class¶
Enum compareTo()¶
- The compareTo() method compares the enum constants based on their ordinal value. For example,
Methods of Java Enum Class¶
Enum toString()¶
- The toString() method returns the string representation of the enum constants. For example,
Methods of Java Enum Class¶
Enum name()¶
- The name() method returns the defined name of an enum constant in string form. The returned value from the name() method is final. For example,
Methods of Java Enum Class¶
Java Enum valueOf()¶
- The
valueOf()
method takes a string and returns an enum constant having the same string name. For example,
Methods of Java Enum Class¶
Enum values()¶
- The
values()
method returns an array of enum type containing all the enum constants. For example,
Why Java Enums?¶
-
In Java, enum was introduced to replace the use of int constants.
-
Suppose we have used a collection of int constants.
- Here, the problem arises if we print the constants.
- It is because only the number is printed which might not be helpful.
Why Java Enums?¶
- So, instead of using int constants, we can simply use enums. For example,
- This makes our code more intuitive.
Why Java Enums?¶
- Also, enum provides compile-time type safety.
- If we declare a variable of the Size type. For example,
- Here, it is guaranteed that the variable will hold one of the four values.
- Now, If we try to pass values other than those four values,
- the compiler will generate an error.
Java enum Constructor¶
- In Java, an enum class may include a constructor like a regular class. These enum constructors are either
- private - accessible within the class or
- package-private - accessible within the package
enum Constructor Example¶
enum Constructor Example¶
enum Constructor Example¶
- In the example, we have created an enum Size.
- It includes a private enum constructor.
- The constructor takes a string value as a parameter and assigns value to the variable pizzaSize.
- Since the constructor is private,
- we cannot access it from outside the class. However,
- we can use enum constants to call the constructor.
- In the Main class, we assigned SMALL to an enum variable size.
- The constant SMALL then calls the constructor Size with string as an argument.
- Finally, we called getSize() using size.
Java enum Strings¶
Java enum Strings¶
In Java, we can get the string representation of enum constants using the toString() method or the name() method. For example,
Change Default String Value of enums¶
- We can change the default string representation of enum constants by overriding the toString() method. For example,
Change Default String Value of enums¶
- In the above program, we have created an enum Size. And we have overridden the
toString()
method for enum constantsSMALL
andMEDIUM
. - Note: We cannot override the
name()
method. It is because thename()
method isfinal
.
Java Abstract Class¶
Java Abstract Class¶
-
An abstract class is a class that created using abstract keyword. In other words, a class prefixed with abstract keyword is known as an abstract class.
-
In java, an abstract class may contain abstract methods (methods without implementation) and also non-abstract methods (methods with implementation).
-
We use the following syntax to create an abstract class.
Java Abstract Class Example-1¶
Java Abstract Class Example-1¶
Java Abstract Class Example-1¶
Java Abstract Class Example-1¶
Java Abstract Class Example-1¶
Java Abstract Class Example-1¶
- An abstract class can not be instantiated but can be referenced.
- That means we can not create an object of an abstract class,
- but base reference can be created.
Java Abstract Class Example-1¶
- In the example program, the child class objects are created to invoke the overridden abstract method.
- But we may also create base class reference and assign it with child class instance to invoke the same.
- The main method of the above program can be written as follows that produce the same output.
Java Abstract Class Example-1¶
Java Abstract Class Example-2¶
Java Abstract Class Example-2¶
Java Abstract Class Example-2¶
Java Abstract Class Example-3¶
Java Abstract Class Example-3¶
Java Abstract Class Example-3¶
Java Abstract Class Example-3¶
Accesses Constructor of Abstract Classes¶
- An abstract class can have constructors like the regular class. And, we can access the constructor of an abstract class from the subclass using the super keyword. For example,
Accesses Constructor of Abstract Classes¶
- Note that the
super
should always be the first statement of the subclass constructor
Java Abstract Class¶
Rules for method overriding¶
An abstract class must follow the below list of rules.
- An abstract class must be created with abstract keyword.
- An abstract class can be created without any abstract method.
- An abstract class may contain abstract methods and non-abstract methods.
- An abstract class may contain final methods that can not be overridden.
Java Abstract Class¶
Rules for method overriding¶
- An abstract class may contain static methods, but the abstract method can not be static.
- An abstract class may have a constructor that gets executed when the child class object created.
- An abstract method must be overridden by the child class, otherwise, it must be defined as an abstract class.
- An abstract class can not be instantiated but can be referenced.
Java Abstract Class Review¶
Java Abstract Class Review¶
The abstract class in Java cannot be instantiated (we cannot create objects of abstract classes). We use the abstract keyword to declare an abstract class. For example,
Java Abstract Class Review¶
- An abstract class can have both the regular methods and abstract methods. For example,
Java Abstract Method Review¶
- A method that doesn't have its body is known as an abstract method. We use the same abstract keyword to create abstract methods. For example,
Here, display() is an abstract method. The body of display() is replaced by ;.
If a class contains an abstract method, then the class should be declared abstract. Otherwise, it will generate an error. For example,
Java Abstract Class and Method Example¶
- Though abstract classes cannot be instantiated, we can create subclasses from it. We can then access members of the abstract class using the object of the subclass.
Java Abstract Class and Method Example¶
Java Abstract Class and Method Example¶
- In the example, we have created an abstract class named Language. The class contains a regular method display().
- We have created the Main class that inherits the abstract class. Notice the statement,
- Here, obj is the object of the child class Main. We are calling the method of the abstract class using the object obj.
Java Abstract Method Review Keypoints¶
- We use the abstract keyword to create abstract classes and methods.
- An abstract method doesn't have any implementation (method body).
- A class containing abstract methods should also be abstract.
- We cannot create objects of an abstract class.
- To implement features of an abstract class, we inherit subclasses from it and create objects of the subclass.
- A subclass must override all abstract methods of an abstract class. However, if the subclass is declared abstract, it's not mandatory to override abstract methods.
- We can access the static attributes and methods of an abstract class using the reference of the abstract class. For example,
Java Object Class¶
Java Object Class¶
- In java, the Object class is the super most class of any class hierarchy.
- The Object class in the java programming language is present inside the java.lang package.
- Every class in the java programming language is a subclass of Object class by default.
- The Object class is useful when you want to refer to any object whose type you don't know.
- Because it is the superclass of all other classes in java,
- it can refer to any type of object.
Methods of Object class¶
- object getClass()
- Returns Class class object
- int hashCode()
-
returns the hashcode number for object being used.
-
boolean equals(Object obj)
- compares the argument object to calling object.
- int clone()
- Compares two strings, ignoring case
Methods of Object class¶
- object concat(String)
- Creates copy of invoking object
- String toString()
- Returns the string representation of invoking object.
- void notify()
- Wakes up a thread, waiting on invoking object's monitor.
- void notifyAll()
- wakes up all the threads, waiting on invoking object's - monitor.
Methods of Object class¶
- void wait()
-
causes the current thread to wait, until another thread - notifies
-
void wait(long,int)
- causes the current thread to wait for the specified - milliseconds and nanoseconds, until another thread notifies.
- void finalize()
- It is invoked by the garbage collector before an object is being garbage collected.
Java Forms of Inheritance¶
- The inheritance concept used for the number of purposes in the java programming language.
- One of the main purposes is substitutability.
- The substitutability means that when a child class acquires properties from its parent class, the object of the parent class may be substituted with the child class object.
-
For example, if B is a child class of A, anywhere we expect an instance of A we can use an instance of B.
-
The substitutability can achieve using inheritance, whether using extends or implements keywords.
Java Forms of Inheritance¶
- The following are the differnt forms of inheritance in java.
- Specialization
- Specification
- Construction
- Extension
- Limitation
- Combination
Java Forms of Inheritance¶
Specialization¶
It is the most ideal form of inheritance. The subclass is a special case of the parent class. It holds the principle of substitutability.
Java Forms of Inheritance¶
Specification¶
This is another commonly used form of inheritance. In this form of inheritance, the parent class just specifies which methods should be available to the child class but doesn't implement them. The java provides concepts like abstract and interfaces to support this form of inheritance. It holds the principle of substitutability.
Java Forms of Inheritance¶
Construction¶
This is another form of inheritance where the child class may change the behavior defined by the parent class (overriding). It does not hold the principle of substitutability.
Java Forms of Inheritance¶
Extension¶
This is another form of inheritance where the child class may add its new properties. It holds the principle of substitutability.
Java Forms of Inheritance¶
Limitation¶
This is another form of inheritance where the subclass restricts the inherited behavior. It does not hold the principle of substitutability.
Java Forms of Inheritance¶
Combination¶
This is another form of inheritance where the subclass inherits properties from multiple parent classes. Java does not support multiple inheritance type.
Benefits and Costs of Inheritance in java¶
- Inheritance is the core and more useful concept of Object-Oriented Programming.
- It proWith inheritance, we will be able to override the methods of the base class so that the meaningful implementation of the base class method can be designed in the derived class.
- An inheritance leads to less development and maintenance costs. Vides many benefits, and a few of them are listed below.
Benefits of Inheritance¶
- Inheritance helps in code reuse. The child class may use the code defined in the parent class without re-writing it.
- Inheritance can save time and effort as the main code need not be written again.
- Inheritance provides a clear model structure which is easy to understand.
- An inheritance leads to less development and maintenance costs.
- With inheritance, we will be able to override the methods of the base class so that the meaningful implementation of the base class method can be designed in the derived class. An inheritance leads to less development and maintenance costs.
- In inheritance base class can decide to keep some data private so that it cannot be altered by the derived class.
Costs of Inheritance¶
- Inheritance decreases the execution speed due to the increased time and effort it takes, the program to jump through all the levels of overloaded classes.
- Inheritance makes the two classes (base and inherited class) get tightly coupled. This means one cannot be used independently of each other.
- The changes made in the parent class will affect the behavior of child class too.
- The overuse of inheritance makes the program more complex.
Defining Packages in java¶
Defining Packages in java¶
- In java, a package is a container of classes,
- interfaces, and
- sub-packages.
- We may think of it as a folder in a file directory.
- We use the packages to
- avoid naming conflicts and
- to organize
- project-related
- classes,
- interfaces, and
- sub-packages into a bundle.
Defining Packages in java¶
- In java, the packages have divided into two types.
- Built-in Packages
- User-defined Packages
Built-in Packages¶
- The built-in packages are the packages from java API. The Java API is a library of pre-defined classes, interfaces, and sub-packages.
-
The built-in packages were included in the JDK.
-
There are many built-in packages in java, few of them are as
java, lang, io, util, awt, javax, swing, net, sql
, etc. -
We need to import the built-in packages to use them in our program.
- To import a package, we use the
import
statement.
User-defined Packages¶
- The user-defined packages are the packages created by the user.
- User is free to create their own packages.
Definig a Package in java¶
- We use the package keyword to create or define a package in java programming language.
Definig a Package in java¶
-
The package statement must be the first statement in the program.
-
The package name must be a single word.
-
The package name must use Camel case notation.
Definig a Package in java¶
- create a user-defined package myPackage
Definig a Package in java¶
- Now, save the example code in a file
DefiningPackage.java
, and compile it using the following command.
-
The above command creates a directory with the package name myPackage, and the
DefiningPackage.class
is saved into it. -
Run the program use the following command.
- When we use IDE like Eclipse, Netbeans, etc. the package structure is created automatically.
Access protection in java packages¶
- In java, the access modifiers define the accessibility of the class and its members.
-
For example, private members are accessible within the same class members only. Java has four access modifiers, and they are default, private, protected, and public.
-
In java, the package is a container of classes, sub-classes, interfaces, and sub-packages. The class acts as a container of data and methods. So, the access modifier decides the accessibility of class members across the different packages.
-
In java, the accessibility of the members of a class or interface depends on its access specifiers.
Access protection in java packages¶
Access protection in java packages¶
-
The public members can be accessed everywhere.
-
The private members can be accessed only inside the same class.
-
The protected members are accessible to every child class (same package or other packages).
-
The default members are accessible within the same package but not outside the package.
Access protection in java packages example¶
Access protection in java packages example¶
Access protection in java packages example¶
Importing Packages in java¶
-
In java, the import keyword used to import built-in and user-defined packages. When a package has imported, we can refer to all the classes of that package using their name directly.
-
The import statement must be after the package statement, and before any other statement.
-
Using an import statement, we may import a specific class or all the classes from a package.
Importing Packages in java¶
-
Using one import statement, we may import only one package or a class.
-
Using an import statement, we can not import a class directly, but it must be a part of a package.
-
A program may contain any number of import statements.
Importing specific class¶
Importing specific class¶
- import a built-in package and Scanner class.
Importing all the classes¶
- Using an importing statement, we can import all the classes of a package. To import all the classes of the package, we use * symbol.
- The following syntax is employed to import all the classes of a package.
Importing all the classes¶
- import a built-in package.
Importing all the classes¶
-
The import statement imports only classes of the package, but not sub-packages and its classes.
-
We may also import sub-packages by using a symbol '.' (dot) to separate parent package and sub-package.
References¶
- BtechSmartClass-super Keyword
- Programiz-super Keyword
- BtechSmartClass-Java final Keyword
- Programiz-final Keyword
- BtechSmartClass-java Polymorphism
- Programiz-Polymorphism
- Programiz-Encapsulation
- BtechSmartClass-Java Method Overriding
References¶
- Programiz-Method Overriding
- Programiz-Nested Inner Class
- Programiz-Static Class
- Programiz-Anonymous Class
- Programiz-enums
- Programiz-enum constructor
- Programiz-enum string
- BtechSmartClass-Java Abstract Class
- Programiz-Abstract Classes Methods
References¶
- BtechSmartClass-Java Object class
- BtechSmartClass-Java Forms of Inheritance
- Programiz-Interfaces
- BtechSmartClass-Java Benefits and Costs of Inheritance
- BtechSmartClass-Java Defining Packages
- BtechSmartClass-Java Access Protection in Packages
- BtechSmartClass-Java Importing Packages
\(End-Of-Week-2-Module\)