0% found this document useful (0 votes)
33 views

Java

There are four main types of classes in Java: abstract classes, static classes, final classes, and interfaces. An abstract class cannot be instantiated but can be subclassed, while a static class can only be nested within another class and cannot access non-static members of the outer class. A final class prevents subclassing and its members cannot be overridden. An interface contains abstract methods and variables and defines common behaviors for unrelated classes. The document also discusses predefined and user-defined methods, instance methods, and method overloading in Java.

Uploaded by

tapcom19
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Java

There are four main types of classes in Java: abstract classes, static classes, final classes, and interfaces. An abstract class cannot be instantiated but can be subclassed, while a static class can only be nested within another class and cannot access non-static members of the outer class. A final class prevents subclassing and its members cannot be overridden. An interface contains abstract methods and variables and defines common behaviors for unrelated classes. The document also discusses predefined and user-defined methods, instance methods, and method overloading in Java.

Uploaded by

tapcom19
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

A closer look at Classes

and Method
• Abstract class
Types of • Static class

classes • Final class


• Interface
Static class

• In Java, static is a keyword that manage objects in the memory. The static object belongs to the class instead
of the instance of the class.
• We can make a class static if and only if it is a nested class. We can also say that static classes are known as
nested classes. It means that a class that is declared as static within another class is known as a static class.
Nested static class does not require reference to the outer class. The purpose of a static class is to provide the
outline of its inherited class.
• The properties of the static class are:
• The class has only static members.
• It cannot access the member (non-static) of the outer class.
• We cannot create an object of the static class.
• A static method belongs to the class rather than the object of a class.
• A static method can be invoked without the need for creating an instance of a
class.
• A static method can access static data member and can change the value of it.
• public class Outer {
• static class Nested_Demo {
• public void my_method() {
• System.out.println("This is my nested class");
• }
• }
• public static void main(String args[]) {
• Outer.Nested_Demo nested = new
Outer.Nested_Demo();
• nested.my_method();
• }
• }
Final Class

• The word final means that cannot be changed. The final class in Java can be
declared using the Final class. Once we declare a class as final, the values
remain the same throughout the program. The purpose of the final class is to
make the class immutable like the String class. It is only a way to make the
class immutable. Remember that the final class cannot be extended. It
also prevents the class from being sub-classed.
• final class ParentClass {
• void showData() {
• System.out.println("This is a method of final Parent class");
• }}
• class ChildClass extends ParentClass {
• void showData() {
• System.out.println("This is a method of Child class");
• }
• }
• class MainClass
• {
• public static void main(String arg[])
• {
• ParentClass obj = new ChildClass();
• obj.showData();
• }
• }
• An abstract class is a that is declared with
the keyword abstract. The class may or
may not contain abstract methods. We
cannot create an instance of an abstract class
but it can be a subclass. These classes are
incomplete, so to complete the abstract class
we should extend the abstract classes to a

Abstract concrete class. When we declare a subclass


as abstract then it is necessary to provide the
implementation of abstract methods.
Class Therefore, the subclass must also be
declared abstract. We can achieve data
hiding by using the abstract class. An
example of an abstract class
is AbstarctMap class that is a part of the
Collections framework.
• An interface in Java is a blueprint of a
class. It has static constants and abstract
methods.
• The interface in Java is a mechanism to
achieve abstraction. There can be only
abstract methods in the Java interface, not
method body. It is used to achieve
Interface abstraction and multiple inheritance in Java.
• In other words, you can say that interfaces
can have abstract methods and variables. It
cannot have a method body.
1. interface Drawable{
2. void draw();
3. }
4. //Implementation: by second user
5. class Rectangle implements Drawable{
6. public void draw(){System.out.println("drawing rectangle");}
7. }
8. class Circle implements Drawable{
9. public void draw(){System.out.println("drawing circle");}
10.}
11.//Using interface: by third user
12.class TestInterface1{
13.public static void main(String args[]){
14.Drawable d=new Circle();//
In real scenario, object is provided by method e.g. getDrawable()
15.d.draw();
16.}}
Predefined Methods
• As the name gives it, predefined methods in Java are the ones that the
Java class libraries already define. This means that they can be called
and used anywhere in our program without defining them. There are
numerous predefined methods, such as length(), sqrt(), max(), and
print(), and each of them is defined inside their respective classes.
User defined methods
• Custom methods defined by the user are known as user-defined
methods. It is possible to modify and tweak these methods according
to the situation. The method written by the user or programmer.
1.//user defined method
2.public static void findEvenOdd(int num)
3.{
4.//method body
5.if(num%2==0)
6.System.out.println(num+" is even");
7.else
8.System.out.println(num+" is odd");
9.}
Instance Method

• The method of the class is known as an instance method. It is a non-


static method defined in the class. Before calling or invoking the
instance method, it is necessary to create an object of its class. These
objects have attributes associated with them (member variables) and
methods that usually refer to these member variables.
Overloading Methods

Method Overloading – two or Java uses the type and/or


more methods within the same number of arguments to
class that share the same name, determine which version of the
but their parameter declarations overloaded method to actually
(type and/or number) are call – return type alone is
different – one of the ways that insufficient to distinguish two
Java supports polymorphism versions of a method

You might also like